MCP servers: a catalog server, a host, the model in the middle¶
Run it, don't just read it
Every lab runs on your laptop with Ollama. Set up once (INSTALL), then:
git clone https://github.com/JSWOne/ai-learning.git && cd ai-learning
cd mcp-server && python 01_list_capabilities.py
Lab files and data: mcp-server/ on GitHub. In VS Code, open the repo folder and use Terminal > Run Task > Run this lab.
Small, runnable examples of the Model Context Protocol (MCP) over the same fake B2B catalog used in ../llm-api. Each numbered file shows one pattern.
New to MCP? Read learn-mcp-server.md first: what it is and why, the mental model in code and diagrams, the labs in three stages, key decisions and advanced topics.
Before you start: llm-api (the tool loop); lab 07 also uses the search from rag. After it, you can: publish tools once and use them from any AI client.
Done with the labs? Try the three exercises and the build-it-yourself task in PRACTICE.md.
The catalog server lives in b2b_catalog/. The host that drives qwen3:8b is host.py. A second server (docs_server.py) publishes the RAG search from ../rag. All three are Python and run as separate processes: MCP is the contract between them, so any of them could be rewritten in another language without the others noticing.
Setup¶
Install steps for all topics are in ../INSTALL.md. Nothing beyond the Python packages. From the ai-learning folder:
source .venv/bin/activate # the one venv at ai-learning/ (see ../INSTALL.md section 4)
uv pip install -r requirements.lock # all topics, the tested versions
cd mcp-server && pytest -q && python 01_list_capabilities.py
Run every example from this folder. 06 needs the HTTP server in a second terminal (python -m b2b_catalog.http_app), so you can watch it log each caller; later topics start it for you. 07 needs the RAG setup from ../rag.
Files¶
Track: Core labs are the Core path through the course. Before you ship: safety lessons to do before anything you build goes to real users. Go further: when your work needs that pattern (see Use it when).
| File | Track | Pattern | Use it when | Time |
|---|---|---|---|---|
01_list_capabilities.py |
Core | Connect over stdio and list tools, resources, prompts. No model | You want to see what a host sees | seconds |
02_call_tools_directly.py |
Go further | Call tools and read resources by hand: success, business error, schema error, unknown tool | Testing a server before any model touches it | seconds |
03_ollama_host.py |
Core | qwen3:8b uses the server's tools through the host loop | The core pattern: MCP tools in your own agent | ~1 min |
04_prompts_and_resources.py |
Go further | Server-side prompt (RFQ to quote) plus resources attached as context | Reusable workflows and context, not just tools | ~1-2 min |
05_approval_policies.py |
Go further | Approval from tool annotations: reads run, orders wait for a human | Anything with write tools | ~1 min |
06_http_and_identity.py |
Go further | Streamable HTTP, bearer token decides the customer | A shared, deployed server | seconds |
07_multi_server.py |
Go further | Catalog server + docs server, prefixed tool names | Several teams, several servers, one assistant | ~1-2 min |
Shared code:
| File | What it holds |
|---|---|
b2b_catalog/catalog.py |
Fake data and business logic (stock, tiers, credit, quotes, orders). No MCP in it |
b2b_catalog/server.py |
create_server(customer_id): 7 tools, 4 resources (one a template), 1 prompt |
b2b_catalog/stdio.py, b2b_catalog/http_app.py |
The two transports. Identity from an env var (stdio) or a token (HTTP) |
host.py |
connect(), run_mcp_agent(), schema translation (flat_schema), approval policies, ensure_http_server() |
docs_server.py |
MCP server wrapping ../rag search, tier bound at launch |
Exercises to practise with: PRACTICE.md (your own files go in my-work/). Questions and answers are in FAQ.md. The learning guide is learn-mcp-server.md (diagrams in diagrams/).
Try it in other clients¶
MCP Inspector (a web UI for any server; optional, needs Node.js): npx @modelcontextprotocol/inspector python -m b2b_catalog.stdio from this folder, then open the URL it prints and click through tools, resources and prompts.
Claude Desktop (optional, uses your Claude app, not an API key): Settings → Developer → Edit Config, add the block below with your own paths, restart the app. The same server now works there unchanged.
{
"mcpServers": {
"b2b-catalog": {
"command": "/Users/<you>/Documents/Learning/ai-learning/.venv/bin/python",
"args": ["-m", "b2b_catalog.stdio"],
"env": { "MCP_CUSTOMER_ID": "C-102", "PYTHONPATH": "/Users/<you>/Documents/Learning/ai-learning/mcp-server" }
}
}
}
Use the venv's Python, not python3, so the server finds the mcp package. PYTHONPATH tells it where the b2b_catalog package is.
Rules of thumb¶
- Keep business logic out of the MCP layer (
catalog.pyhas no MCP in it). The server is a thin adapter you can test without a model. - Identity comes from the connection (launch env, token), never from a tool argument the model fills in.
check_credithas nocustomer_id. - Return business failures as
isErrorresults with a clear message (raiseToolError). Let the SDK reject bad arguments against the schema. - Annotate every tool (
read_only_hint,destructive_hint;readOnlyHinton the wire). The host turns them into an approval policy; a human approves anything that commits money or stock. - Annotations are hints. Trust them from your own servers only; use an allow-list for third-party servers.
- On stdio, stdout is the protocol. Log to stderr, and silence libraries that print.
- One system of record per fact. Prices come from the pricing code; documents only explain the rules. Point to the source instead of copying it into a second server (07 showed what happens when two servers disagree).
- Descriptions are prompts, same as in llm-api. Few, well-named tools beat many thin ones; prefix names when you combine servers.
- Start with stdio for local and desktop use. Move to Streamable HTTP, stateless, behind real auth when the server is shared.
Tests¶
pytest -q runs two model-free test files. test_server.py: the business rules, then an in-memory MCP client checks tools, annotations, errors, identity binding, resources and prompts.
test_host.py: schema translation, approval policies, the loop against the real server (a child process) with a scripted fake model, and the HTTP server with its tokens.
Which tools qwen3:8b actually picks can only be checked by running 03, 04, 05 and 07 against Ollama.