> ## Documentation Index
> Fetch the complete documentation index at: https://docs.gloo.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Lesson 4: Tools and MCP

> Let a model call your own functions with tool use, run the full tool loop on Gloo, and connect your coding assistant to Gloo's docs with the Model Context Protocol.

## Letting the Model Act

On its own, a model can't act on anything outside itself. It can't check today's calendar, look up an order or add up a spreadsheet. Ask it anyway and it may guess.

There are two places you'll want to give a model more reach: in the app you're shipping, so it can act on live data, and in the editor you're building with, so it stops guessing about APIs. This lesson covers both:

* **Part 1: Tools in your app.** With **tool use** (also called **function calling**), you describe functions your app can run. When the model needs one, it asks your app to run it, your app sends back the result, and the model uses that result in its answer. The model decides *what* to do; your code does it.
* **Part 2: MCP in your editor.** **MCP** is an open standard for connecting AI tools to outside sources. You'll use it to give your coding assistant Gloo's docs, so its answers about the API stay current.

***

## Core Concepts

### Part 1: Tools in Your App

#### The Tool Loop

In its simplest form, the loop has five steps and two requests:

1. **You send the question and your tool definitions.** Each tool has a `name`, a `description` and `parameters`, a JSON Schema for its inputs (the same kind of schema as in Lesson 3).
2. **The model replies with a tool call** instead of an answer: the tool's name and the arguments it wants to use.
3. **Your app runs the function** with those arguments.
4. **You send a second request** with the conversation so far, the model's tool call and your result.
5. **The model writes its answer** using the result.

The model can also ask for several tools at once, or for another tool after it sees a result. Run every call it asks for, and repeat steps 3 and 4 until it answers.

The model never runs anything itself: your app acts, so you decide what's allowed.

#### Writing Good Tool Definitions

The model chooses tools by reading your descriptions, so write them for the model:

* **Name** says what it does: `get_service_times`, not `tool1`.
* **Description** says when to use it, in a sentence or two.
* **Parameters** are as specific as you can make them: use `enum` for a fixed set of values, and mark what's `required`.

#### Choosing When Tools Are Used

`tool_choice` controls whether the model must use a tool. Leave it out and the model decides. Set it to `"required"` to make the model call one, which is how Lesson 3 got structured output.

### Part 2: MCP in Your Editor

#### What MCP Is

The [Model Context Protocol](https://modelcontextprotocol.io/) (MCP) is an open standard that lets AI tools, such as Claude, Cursor, VS Code and ChatGPT, connect to outside data sources and search them directly.

Gloo hosts an MCP server for its documentation at `https://docs.gloo.com/mcp`. Connect your coding assistant to it, and when you ask how to call a Gloo endpoint, it looks up the current docs instead of relying on what it learned in training. So its answers about model IDs, fields and URLs come from the docs as they are today.

***

## Get Started

### Part 1: Run the Tool Loop

This example gives the model one tool, runs each function it asks for, and sends back the results until the model answers. It uses the guarded Responses endpoint (with Gloo's guardrails, priced at the model's rate plus 6.5%), as in the [Tool Use guide](https://docs.gloo.com/api-guides/tool-use.md).

```bash theme={null}
pip install openai
```

```python Python theme={null}
import json
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["GLOO_API_KEY"],
    base_url="https://platform.ai.gloo.com/ai/v2/guarded",
)

MODEL = "gloo-anthropic-claude-sonnet-4.6"

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_service_times",
            "description": "Get the service times for a campus on a given day.",
            "parameters": {
                "type": "object",
                "properties": {
                    "campus": {"type": "string", "enum": ["north", "south"]},
                    "day": {"type": "string", "description": "Day of the week, such as Sunday"},
                },
                "required": ["campus", "day"],
            },
        },
    }
]


def get_service_times(campus, day):
    # In a real app, look this up in your database.
    times = {"north": ["9:00", "11:00"], "south": ["10:30"]}
    return {"campus": campus, "day": day, "times": times.get(campus, [])}


conversation = [
    {"role": "user", "content": "When are the Sunday services at the north campus?"}
]

for _ in range(5):  # a limit, so a confused model can't loop forever
    # 1. The model answers or asks for one or more tools.
    response = client.responses.create(model=MODEL, input=conversation, tools=tools)
    calls = [item for item in response.output if item.type == "function_call"]
    if not calls:
        print(response.output_text)  # the model answered
        break

    for call in calls:
        # 2. Your app runs each function.
        result = get_service_times(**json.loads(call.arguments))

        # 3. Send each call and its result back, then go around again.
        conversation += [
            {"type": "function_call", "call_id": call.call_id, "name": call.name, "arguments": call.arguments},
            {"type": "function_call_output", "call_id": call.call_id, "output": json.dumps(result)},
        ]
```

The first response contains a `function_call` item like this:

```json theme={null}
{
  "type": "function_call",
  "call_id": "call_abc123",
  "name": "get_service_times",
  "arguments": "{\"campus\": \"north\", \"day\": \"Sunday\"}"
}
```

The `call_id` ties your `function_call_output` to the call it answers, so both items need the same one.

### Part 2: Connect Your Coding Assistant to Gloo's Docs

In Claude Code, one command adds the docs server:

```bash theme={null}
claude mcp add --transport http gloo-ai-docs https://docs.gloo.com/mcp
```

In Cursor, add it to `mcp.json`:

```json theme={null}
{
  "mcpServers": {
    "gloo-ai-docs": {
      "url": "https://docs.gloo.com/mcp"
    }
  }
}
```

Then ask your assistant something like "What models does Gloo AI support?" and it searches the docs to answer. The [MCP Integration](https://docs.gloo.com/api-guides/mcp-integration.md) guide has steps for VS Code and Claude too, plus one-click setup from the menu at the top of any docs page.

***

## Good to Know

* **Use the nested tool schema.** Define tools as `{"type": "function", "function": { ... }}` on both the Responses and Chat Completions endpoints. The flat Responses tool schema isn't reliably accepted across providers. In TypeScript, the SDK's types expect the flat schema, so the [Tool Use guide](https://docs.gloo.com/api-guides/tool-use.md#defining-tools) adds `// @ts-ignore`.
* **Pin the model.** Tool calls behave most consistently when the same model handles every call. Avoid auto-routing for tool workflows.
* **Check the model supports tools.** The catalog's `supports_tools` flag tells you. See [Supported Models](https://docs.gloo.com/api-guides/supported-models.md).
* **Validate arguments before acting.** Treat the arguments like any user input. Check them before you query a database or send a message.
* **Don't repeat side effects.** If a request is retried after a tool already ran, a tool that sends an email could send it twice. Give each tool run an ID so a repeat can be spotted and skipped. [Handling Streaming Failures](https://docs.gloo.com/best-practices/completions-streaming-failures.md#tool-calls-and-side-effects) covers this.

***

## Key Takeaway

Tool use lets the model ask your app to do things it can't do alone: fetch live data, look things up, run exact calculations. You define the tools, the model asks for them, your code runs them and sends back the result. In your editor, MCP lets your coding assistant search Gloo's docs, so its answers about the API stay current.

> [!TIP]
>
> **Try it on Gloo:** the [Tool Use guide](https://docs.gloo.com/api-guides/tool-use.md) shows tool calls on both the Responses and Completions endpoints, and [MCP Integration](https://docs.gloo.com/api-guides/mcp-integration.md) connects Gloo's docs to Claude, Cursor or VS Code in a minute.

***

## Learn more

* [Tool Use](https://docs.gloo.com/api-guides/tool-use.md): tool definitions, tool calls and multi-turn results on each endpoint
* [Completions with Tool Use](https://docs.gloo.com/tutorials/completions-tool-use.md): a complete tool-use project in several languages
* [MCP Integration](https://docs.gloo.com/api-guides/mcp-integration.md): connect Gloo's docs to your AI tools

***

## What's next

Next, you'll see how a coding agent runs this tool loop again and again to finish a whole task, and where a person approves what it does.

> [card] [Lesson 5: Coding Agents](https://docs.gloo.com/ai-learning-center/gloo-ai-104/coding-agents.md)
>
> How an agent plans, acts and checks its work.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.