Skip to content
Forward Deployed

Forward Deployed
T12 exercise

Exercise: the course retriever as an MCP server

Track
Agent Protocols: MCP & A2A
By
Sourav Banerjee
Last updated
30 Nov 2026

Report an issue

01 Context

Problem statement

Larkspur Cloud has three assistants that each copy the handbook retriever into their own code. You move the retriever behind one MCP server, so every assistant calls the same tool. Before anyone switches, you prove two things with numbers: the protocol returns the same passages as the function it wraps, and an agent calling the tool still finds the evidence.

The rule you apply comes from lesson 5 of T12.3: once retrieval is an MCP tool, the server owns chunking and ranking, and the host's model owns the query and how many chunks to ask for. Every argument the tool exposes is a decision you hand to the model. Keep an argument only if the model uses it well on your questions.

02 Outcomes

What you will learn

  • Wrap a Python retriever as a FastMCP tool and read the tool definition it generates.
  • Check that a tool called over MCP returns the same passages as the function, on the 25 scored golden questions.
  • See what a client loses when it reads only the first content block of a list result.
  • Count server starts for a held session against a session per call over stdio.
  • Run an agent over the tool and decide which retrieval argument the server should keep for itself.

03 Setup

Before you start

Lessons

Dataset

The Larkspur Cloud handbook (13 files) and its 30-question golden set, plus agent_questions.jsonl: 10 of the golden questions with the key facts a right answer must hold. All in data/. The direct calls are scored on the 25 questions with evidence spans.

Code

load_handbook, load_jsonl, tfidf_retriever, hit_rate, recall_at_k, mrr and is_hit from src/lab_kit.py. FastMCP and ClientSession from the mcp SDK, load_mcp_tools and MultiServerMCPClient from langchain-mcp-adapters, create_agent from LangChain and ChatOllama from langchain-ollama.

Environment

The course's default environment (uv run jupyter lab) with Ollama running locally and qwen2.5:3b pulled (llama3.2:3b too for the extension). Everything runs offline: TF-IDF retrieval, local models and no API keys. The module labs call a hosted chat model. Here a 3B model on Ollama stands in for it, so expect more mistakes than a large model makes.

04 Architecture

Experiment workflow

The corpus, the chunking, the ranking and the questions stay fixed. The first rows change only how the tool is reached. The agent rows change only whether the model may choose k.

Diagram: Load the handbook and the golden set; FastMCP server: search_handbook over TF-IDF; In-process reference, k = 4; Held session: 25 direct calls; Session per call: 25 direct calls; Hit rate, recall at 4 and MRR for each row; Agent, model picks k; Agent, server fixes k = 4; Per question: k sent, evidence seen, key facts in the answer; Keep or drop the k argumentDiagram: Load the handbook and the golden set; FastMCP server: search_handbook over TF-IDF; In-process reference, k = 4; Held session: 25 direct calls; Session per call: 25 direct calls; Hit rate, recall at 4 and MRR for each row; Agent, model picks k; Agent, server fixes k = 4; Per question: k sent, evidence seen, key facts in the answer; Keep or drop the k argument

Extension: a second model, llama3.2:3b, on the fixed-k server.

05 Build

Implementation tasks

  1. Task 1: Load the data

    Load the handbook, the golden set and agent_questions.jsonl from ../data/.

  2. Task 2: Write the server

    Write handbook_server.py: the recursive splitter at 500 / 50, tfidf_retriever over the chunks, and one FastMCP tool, search_handbook(query, k=4), that returns a list of {"source", "text"} passages. Run it over stdio. Start it from the notebook with stdio_client and one ClientSession, and print the input and output schemas from list_tools().

    @mcp.tool()
    def search_handbook(query: str, k: int = 4) -> list[dict]:
        ...
    
    mcp.run(transport="stdio")
    
  3. Task 3: Check parity

    Call the tool once per scored golden question through one held session, read every passage from structuredContent, and score the results. Score the in-process tfidf_retriever the same way. Then score the same calls again reading only result.content[0].

  4. Task 4: Count sessions

    Make the same 25 calls through the tools from MultiServerMCPClient.get_tools(), which open a session per call. Have the server append its process id to a file named by an environment variable, and count the lines for each row.

    New here: the start counter is four lines in the server. The lessons count processes from recorded traffic instead.

  5. Task 5: Run the agent

    Hold one session, turn its tools into LangChain tools with load_mcp_tools(session), and run create_agent with ChatOllama(model="qwen2.5:3b", temperature=0) on the 10 agent questions, with recursion_limit=8. For each question keep the k and query the model sent, the passages from each ToolMessage's artifact, and whether the answer holds every key fact.

  6. Task 6: Change one thing: the server fixes k

    Start the server with --fixed-k, which registers the same tool without a k argument and always returns 4 passages. Repeat task 05 with the same model, questions and budget, then list the questions where the two agent rows disagree.

  7. Task 7: Decide

    Keep the k argument only if the agent that may choose it finds the evidence as often as the one that may not.

  8. Task 8: Extension: a second model

    Run task 06 again with llama3.2:3b. The server stays the same.

06 Measure

Record results

What you enter stays in this browser only.

RowServer startsTool callsHit rateRecallKey facts in the answerSeconds
In-process reference, k = 4025n/an/a
MCP, held session, every passage25n/a
MCP, held session, first block only25n/an/a
MCP, session per call25n/a
Agent qwen2.5:3b, model picks k
Agent qwen2.5:3b, server fixes k = 4
Agent llama3.2:3b, server fixes k = 4 (extension)

The direct rows are scored on the 25 questions with evidence, and Recall is recall at 4. The agent rows are scored on the 10 agent questions, over every passage the agent saw for a question, whatever k it asked for. Key facts counts answers that hold every key fact.

07 Verify

Done when

0 of 5 done

What you enter stays in this browser only.

08 Debug

Common mistakes

Reading only the first content block

A list result arrives as one content block per item. A client that reads content[0] hands the model one passage of four (lesson 3 of T12.1).

Calling a parity gap a protocol bug

If the MCP row differs from the reference, check the chunking and k first. The protocol moves JSON and does not rank anything.

A session per call over stdio

get_tools() from MultiServerMCPClient starts a server process for every call, so any in-memory index is rebuilt each time (lesson 4 of T12.2). Hold the session.

Writing to stdout in a stdio server

Over stdio, stdout carries the protocol. A stray print in the server breaks the session. Log to stderr or a file.

Scoring the agent on the answer only

An answer can miss a key fact because retrieval missed it or because the model dropped it. Keep the passages the tool returned, so you can tell which.

09 Transfer

Interview angle

“You expose your retriever as an MCP tool. What do you let the calling model control?”

Show a model answer

The query, because only the host knows the question. Chunking, ranking and usually the result count stay on the server, because every host shares them and a small model picks them badly. Measure each exposed argument on a golden set before you publish it.

10 Review

Worked solution

The worked solution is solution/p11_exercise_solution.ipynb (open in Colab), with the server in solution/handbook_server.py. Locally, open it from exercise/solution/ so ../data/ resolves, start Ollama, and run it top to bottom with the default environment. In Colab, clone the repository first and install Ollama in the runtime, because the notebook reads the corpus and src/lab_kit.py from the repository and calls a local model.

Show our run and the reasoning

Our run: qwen2.5:3b and llama3.2:3b through Ollama 0.40.6, mcp 1.30.0, langchain-mcp-adapters 0.3.2, langchain 1.4.7, langchain-ollama 1.1.0 and scikit-learn 1.9.1, offline.

Row Server starts Tool calls Hit rate Recall Key facts in the answer Seconds
In-process reference, k = 4 0 25 0.96 0.94 n/a n/a
MCP, held session, every passage 1 25 0.96 0.94 n/a 5.0
MCP, held session, first block only 1 25 0.84 0.76 n/a n/a
MCP, session per call 26 25 0.96 0.94 n/a 114.9
Agent qwen2.5:3b, model picks k 1 11 0.70 0.65 6/10 63
Agent qwen2.5:3b, server fixes k = 4 1 10 1.00 0.95 9/10 54
Agent llama3.2:3b, server fixes k = 4 (extension) 1 10 1.00 0.95 9/10 62

The held-session row returns the reference's passages in the same order for all 25 questions, and its MRR is 0.89 like the reference's. The protocol moved the results and changed none of them. The first-block row falls to the reference's top-1 passage: one passage of four reaches the model, the defect lesson 3 of T12.1 traces. The session-per-call row returns the same passages but starts 26 server processes, one for get_tools() and one per call. Each start reloads Python and rebuilds the TF-IDF index, which is why 25 calls took 114.9 seconds instead of 5.0.

With k in the tool's schema, qwen2.5:3b asked for one passage on 6 of the 10 questions. It lost g01 (the top passage was about how to request a refund, not how long it takes), g13 (two calls, both at k = 1, both on the wrong section) and g20, and it saw the right passage for g17 but answered without the 30 days. With the server fixing k = 4, the same model found evidence for all 10 questions and gave 9 answers with every key fact. llama3.2:3b on the fixed-k server scored the same 9 out of 10, so the gain came from the server change, not the model.

The k argument is dropped: the tool publishes only query, and the server returns 4 passages. g20 still fails on both models. It needs two sections, the Billing ticket and the support tier's response time, and the 4 passages for the question hold only the first, so the answer says the handbook does not cover it. That is a retrieval limit of one query at k = 4, not a protocol one. The key-fact check is rough: the model-picks-k answer to g11 passes while it also says Larkspur runs "five regions in the EU", so read the answers the notebook prints as well.