Workflow recipe
Build an MCP server for an internal API
Two read-only tools, tested in MCP Inspector and called from Claude Code, Codex or Cursor.
Bookmark
Bookmarks are kept in your free account, so you can find this page again on any device.
Sign in to bookmarkOn this page
To build an MCP server for an internal API, pick two read-only endpoints and design each as a tool, write the server in Python with FastMCP, and test the tools in MCP Inspector. Then register the server with your coding agent and refine the tool descriptions until the agent picks the right tool without being told which one.
The Model Context Protocol (MCP) lets an agent call tools that live outside it. Wrapping an internal API in a small MCP server means any MCP client (Claude Code, Codex, Cursor, Claude Desktop) can look things up in your systems without you pasting data into the chat.
This recipe builds a server with two read-only tools over a support ticket API: one to search tickets and one to fetch a ticket by id. Swap in your own endpoints; the structure stays the same. It uses FastMCP, a Python framework whose first version became part of the official MCP Python SDK.
NotebookAbout 15 min
1. Choose two read-only endpoints and design the tools
Start read-only. Writing tools (create, update, delete) need more care around confirmation and permissions, and you can add them once the read path works.
For each tool, write down the name, what it is for, its inputs and what it returns. The model chooses tools from their names and descriptions, so write them for a reader who knows nothing about your API.
search_tickets(query: str, status: "open" | "closed" | "any" = "open", limit: int = 10)
Find support tickets whose subject or body matches the query.
Returns id, subject, status, customer and updated time for each match.
Backed by: GET /tickets?q=...&status=...&limit=...
get_ticket(ticket_id: str)
Fetch one ticket with its full message history.
Backed by: GET /tickets/{ticket_id}Return only the fields an agent needs. Large raw JSON responses fill the context window and make answers worse.
TerminalAbout 10 min
2. Create the project
uv init support-mcp
cd support-mcp
uv add fastmcp httpxThe server reads the API location and token from environment variables. Do not hard-code tokens in the file.
EditorAbout 30 min
3. Write the server
import os
from typing import Literal
import httpx
from fastmcp import FastMCP
API_BASE_URL = os.environ["SUPPORT_API_URL"] # e.g. https://support.internal.example.com/api
API_TOKEN = os.environ["SUPPORT_API_TOKEN"]
mcp = FastMCP("support-tickets")
def _client() -> httpx.Client:
return httpx.Client(
base_url=API_BASE_URL,
headers={"Authorization": f"Bearer {API_TOKEN}"},
timeout=15.0,
)
@mcp.tool
def search_tickets(
query: str,
status: Literal["open", "closed", "any"] = "open",
limit: int = 10,
) -> list[dict]:
"""Find support tickets whose subject or body matches the query.
Returns id, subject, status, customer and last update time for each match,
newest first. Use get_ticket for the full conversation.
"""
limit = max(1, min(limit, 25))
params = {"q": query, "limit": limit}
if status != "any":
params["status"] = status
with _client() as client:
resp = client.get("/tickets", params=params)
resp.raise_for_status()
tickets = resp.json()["tickets"]
return [
{
"id": t["id"],
"subject": t["subject"],
"status": t["status"],
"customer": t["customer"]["name"],
"updated_at": t["updated_at"],
}
for t in tickets
]
@mcp.tool
def get_ticket(ticket_id: str) -> dict:
"""Fetch one support ticket by id, including every message in order."""
with _client() as client:
resp = client.get(f"/tickets/{ticket_id}")
if resp.status_code == 404:
raise ValueError(f"No ticket with id {ticket_id}")
resp.raise_for_status()
t = resp.json()
return {
"id": t["id"],
"subject": t["subject"],
"status": t["status"],
"customer": t["customer"]["name"],
"messages": [
{"from": m["author"], "at": m["created_at"], "text": m["body"][:4000]}
for m in t["messages"]
],
}
if __name__ == "__main__":
mcp.run() # stdio transport by default, which is what local agents expectFastMCP builds each tool’s input schema from the type hints and uses the docstring as the description. Raising an exception returns an error result to the agent instead of crashing the server. The field names in the JSON (tickets, customer, messages) are placeholders for whatever your API returns.
Terminal and browserAbout 25 min
4. Test it in MCP Inspector
MCP Inspector is the official tool for poking at a server by hand. It starts your server as a child process, so pass the environment variables with -e:
npx @modelcontextprotocol/inspector \
-e SUPPORT_API_URL=https://support.internal.example.com/api \
-e SUPPORT_API_TOKEN="$SUPPORT_API_TOKEN" \
-- uv run server.pyIn the web UI, list the tools, read the generated schemas and descriptions as the model will see them, and call each tool with a real query, an empty result and a bad id.
For a repeatable check, use the CLI mode, which prints JSON and exits non-zero on failure:
npx @modelcontextprotocol/inspector --cli \
-e SUPPORT_API_URL=https://support.internal.example.com/api \
-e SUPPORT_API_TOKEN="$SUPPORT_API_TOKEN" \
--method tools/call --tool-name search_tickets --tool-arg query=refund --tool-arg limit=3 \
uv run server.pyInspector v2 changed some command-line rules from v1. If an argument is not picked up, check the Inspector README for the -- separator rules.
TerminalAbout 10 min
5. Connect it to your agent
Use an absolute path with uv run --directory so the agent can start the server from any working directory.
claude mcp add --env SUPPORT_API_URL=https://support.internal.example.com/api \
--env SUPPORT_API_TOKEN="$SUPPORT_API_TOKEN" \
--transport stdio support-tickets \
-- uv run --directory /absolute/path/to/support-mcp server.py
claude mcp get support-tickets # check the statusKeep an option such as --transport stdio between the last --env and the server name, otherwise the CLI reads the name as another environment pair. Inside a session, /mcp shows connected servers and their tools.
codex mcp add support-tickets \
--env SUPPORT_API_URL=https://support.internal.example.com/api \
--env SUPPORT_API_TOKEN="$SUPPORT_API_TOKEN" \
-- uv run --directory /absolute/path/to/support-mcp server.py
codex mcp listCodex stores the entry in ~/.codex/config.toml. Inside a session, /mcp lists the tools Codex can call.
Agent chatAbout 30 min
6. Use it from the agent and refine the descriptions
Try a few real questions and watch which tool the agent picks and with what arguments.
If the agent calls the wrong tool, passes odd arguments or makes repeated calls, the fix is almost always in the tool names, docstrings or parameter types. Tighten them, restart the agent session so it reloads the tool list, and try again.
Check it worked
Verification checklist
0 of 6 done
When it goes wrong
If the agent shows the server as failed, run the exact command from your config in a terminal. A missing environment variable raises a KeyError on start-up, and uv may need to install dependencies on first run.
If Inspector connects but tool calls fail with authentication errors, the environment variables did not reach the child process. Pass them with -e; Inspector does not forward your whole shell environment.
If the agent ignores the tools and guesses instead, the descriptions probably do not match the user’s words. Mention the domain (“support tickets”, “customer requests”) in the tool docstrings.
If responses are slow or cut off, return fewer fields, cap list sizes (as limit does above) and truncate long text. Some clients also cap tool output size.
Variations
To share the server with your team, add it with --scope project in Claude Code, which writes .mcp.json at the repository root. Each person still supplies their own token through the environment; never commit tokens in that file.
For a server many people use, run it over HTTP (FastMCP supports the Streamable HTTP transport) behind your normal authentication, and add it to agents with claude mcp add --transport http. Secure a remote MCP server covers OAuth and scoped tokens for that setup, and what changed in MCP 2026-07-28 explains the current revision of the protocol.
To start from a guided template instead, the mcp-builder skill walks an agent through designing and building an MCP server, including evaluation questions for the tools.
Related MCP tutorials
New to the protocol? Start with MCP explained and the step-by-step Python MCP server tutorial. Before other people rely on your server, read MCP security risks, and see connecting MCP servers to your agent for other clients. The MCP in production learning path puts all of these in order. More recipes are in the AI workflow examples hub.
Outcome and tools
- Outcome
- A local MCP server with two tools, tested in Inspector and used from an agent.
- For
- Developers
- Variants for
- Claude Code, Codex
- Agent Skills
Related
- MCP explained: hosts, clients, servers and transports
What MCP is and how the Model Context Protocol connects AI apps to tools and data: hosts, clients, servers, transports and what the 2026-07-28 spec changed.
- Build an MCP server in Python with FastMCP
Build a small MCP server in Python with FastMCP, test its tools in MCP Inspector without a model, then connect it to Claude Code, Cursor or another MCP host.
- Build an MCP server in TypeScript
Build a small MCP server with the official TypeScript SDK v2, test it from the command line and in MCP Inspector, then connect it to Claude Code or Cursor.
- MCP server
A program that exposes tools, resources or prompts to MCP clients, running locally over stdio or remotely over HTTP, so any MCP host can use them.
- Secure a remote MCP server with OAuth
Protect a Streamable HTTP MCP server with OAuth 2.1 as the 2026-07-28 spec requires: resource metadata, audience checks, per-tool scopes and Inspector tests.