MCP FAQ¶
Short answers, with pointers to the code. New questions are added as they come up.
Basics¶
What is MCP, and how is it different from tool calling?¶
- Tool calling is the model asking your code to run a function (
../llm-api). It is a feature of the model API. - MCP is a protocol for publishing those functions from a separate server, so any AI app can discover and call them.
- The model still does plain tool calling. The host translates MCP tools into the model's tool format (
to_tool_spec()inhost.py). - Rule of thumb: one app with its own tools, use tool calling. Same tools for several apps or teams, use MCP.
Why is it called Model Context Protocol?¶
| Word | Meaning | In this folder |
|---|---|---|
| Model | The LLM it serves. The server never talks to the model; the host does | qwen3:8b behind host.py |
| Context | Everything the model gets beyond the user's words: tools, resources, prompts | The 7 tools, 4 resources, 1 prompt from 01 |
| Protocol | A wire contract (JSON-RPC 2.0 messages, a handshake, capability negotiation), not a library | Host and server are separate processes that agree only on this |
It is modelled on the Language Server Protocol (LSP), which lets any editor use any language's tooling. MCP does the same for AI apps and data sources. Anthropic introduced it as an open standard in November 2024.
Host, client, server: who is who?¶
| Role | In this folder | Owns |
|---|---|---|
| Host | host.py, Claude Desktop, an IDE |
The model, the loop, approval, which servers to trust |
| Client | mcp.Client inside the host, one per server |
One connection: discovery and calls |
| Server | b2b_catalog/server.py, docs_server.py |
Tools, resources, prompts, and its data |
Tools, resources, prompts: when do I use which?¶
| Primitive | Who decides to use it | Example here |
|---|---|---|
| Tool | The model | get_price, create_quote, place_order |
| Resource | The app or user, as context | policy://credit-terms, account://me, catalog://products/{sku} |
| Prompt | The user, as a template | rfq_to_quote |
Most servers start with tools only. Add resources when the host should attach context without spending a tool call (04_prompts_and_resources.py).
Does the model know it is talking to an MCP server?¶
No. It sees a list of tools with names, descriptions and JSON Schemas, exactly as in llm-api. MCP sits between the host and the server; the model is unchanged (run_mcp_agent() vs run_agent()).
Why does the host change the server's JSON Schema?¶
- Python SDKs build schemas with pydantic: nested types become
$defsplus$ref, every field gets atitle, optional arguments becomeanyOf [X, null]. - That is valid JSON Schema, but small models follow a flat schema more reliably, and every title costs tokens on every call.
flat_schema()inhost.pyinlines the refs, drops the titles and turns optionalX | Noneinto plainX. Same meaning, less to read.- The server keeps its full schema: other clients may want it. Adapting it to one model is the host's job.
How does the model decide which tool to call first?¶
- There is no planner or dependency graph. Each turn the model reads the question, the tool descriptions and the results so far, and picks the next call (the loop in
run_mcp_agent()). - Descriptions steer it:
search_productssays "Use this when the buyer does not give a SKU", so "nitrile gloves size M" starts with a search, and "HELMET-RED" goes straight toget_price(03). - Independent calls can come in one turn; dependent ones need separate turns, because the result isn't known yet.
- If it skips a step,
get_stockfails with "Unknown SKU" and the model can recover, if the error says how. - When order must be guaranteed, enforce it with data, not hope:
place_orderneeds aquote_idthat onlycreate_quoteproduces. Or combine steps into one tool, or code the workflow.
Why is it called run_mcp_agent? What makes it an agent?¶
- An agent is a model in a loop that chooses its own next action from the results so far, until it decides it is done.
- "mcp" because its tools come from MCP servers;
run_agent()in../llm-api/agent.pyis the same loop with local functions. - Its parts map to the arguments: goal (
task), instructions and context (system), tools (hub), guardrail (approve), budget (max_steps). - Not an agent: a fixed pipeline where your code decides the steps (
../rag/04_basic_rag.py). - Use an agent only when the steps aren't known in advance; otherwise a pipeline is cheaper, faster and easier to test.
Why is the server in Python? Can I write one in TypeScript?¶
- One language for the whole course: no Node.js to install, one venv, one test command.
- The server still runs as its own process and speaks only JSON-RPC, so the host can't tell which language it is (
01). - In practice each team writes its server in its own stack. TypeScript has the largest set of public servers.
- The same
get_stocktool with the official TypeScript SDK (@modelcontextprotocol/sdkandzod):
const server = new McpServer({ name: "b2b-catalog", version: "1.0.0" });
server.registerTool(
"get_stock",
{
description: "Get units currently in stock for a product SKU.",
inputSchema: { sku: z.string().describe("Product SKU, for example BOLT-M8-40") },
annotations: { readOnlyHint: true },
},
async ({ sku }) => ({ content: [{ type: "text", text: JSON.stringify(getStock(sku)) }] }),
);
await server.connect(new StdioServerTransport());
To use it from host.py, point StdioServerParameters at npx tsx server.ts instead of python -m b2b_catalog.stdio. Nothing else changes.
Transports and identity¶
stdio or Streamable HTTP?¶
| stdio | Streamable HTTP | |
|---|---|---|
| Runs | As a child process of the host, one per user | Once, shared by many users |
| Identity | Fixed at launch (env var) | Per request (token) |
| Use for | Desktop apps, IDEs, local tools | Deployed, shared servers |
| Here | b2b_catalog/stdio.py, 01–05, 07 |
b2b_catalog/http_app.py, 06 |
How does the server know which customer is calling?¶
- Never from a tool argument: the model could put any customer ID there.
- stdio: the host sets
MCP_CUSTOMER_IDwhen it launches the server (catalog_server()inhost.py). - HTTP: the server maps the bearer token to a customer before any MCP code runs (
ByTokeninb2b_catalog/http_app.py). Production uses OAuth 2.1. - The tools receive
customer_idfromcreate_server(), a closure the model can't reach. - See
06_http_and_identity.py: same call, two tokens, two answers.
Why is the HTTP server stateless?¶
Stateless mode keeps no session between requests, and each customer's requests go to a server bound to that customer, so there is no session store, no sticky load balancing, and nothing shared between users by mistake. Use sessions only when you need server-to-client messages mid-call (progress, sampling, elicitation).
Safety¶
Are tool annotations a security control?¶
- No. They are hints the server publishes about itself:
readOnlyHint,destructiveHint,idempotentHint,openWorldHint(read_only_hintand so on in Python). - Your host uses them to choose a policy (
ask_for_writes,ask_for_destructiveinhost.py; table in05). - A malicious server can label a delete as read-only. For third-party servers, keep your own allow-list per tool.
- Missing annotations should be treated as "may write, may destroy" (the spec's defaults).
is_destructive()does this.
What are the main MCP security risks?¶
- Tool poisoning: a server's tool description contains instructions to the model ("also send the customer list to..."). Review descriptions; pin server versions.
- Prompt injection through results: data returned by a tool tells the model what to do. Treat results as data.
- Confused deputy: a server acts with its own broad credentials on behalf of any caller. Bind identity per caller (
06). - Too much power: one server with read and write on everything. Split read and write, and gate writes in the host (
05). - Supply chain:
npx some-mcp-serveroruvx some-mcp-serverruns someone else's code on your machine. Treat it like any dependency.
What happens when the model sends bad arguments?¶
The SDK validates them against the tool's schema before your handler runs and returns an isError result the model can read (02, case 3). Business rules (unknown SKU, credit exceeded) are your code's job and come back the same way.
Scaling up¶
How many tools or servers can one host handle?¶
- The model sees every tool from every server, so the llm-api limit still applies: qwen3:8b chooses worse past roughly 10 to 15 tools.
- Connect only the servers a task needs, and route by intent first (
../llm-api/07_tool_routing.py). - Prefix tool names per server so they can't collide (
connect()inhost.py,07).
What MCP features do these labs skip?¶
Sampling (the server asks the host's model to generate text), elicitation (the server asks the user a question mid-call), roots, progress notifications, resource subscriptions and long-running tasks. See "Going deeper" in learn-mcp-server.md.