# MCP agent: answer questions through an MCP server

Source: https://docs.surogate.ai/engine/examples/kits/mcp/

Qwen3.5-2B learns to answer questions about a music store by calling the tools of an
[MCP](https://modelcontextprotocol.io) server: list the tables, read their columns, write
the query, give the answer. 30 GRPO steps on one GPU take it from **48.4% to 81.5%** on
184 held-out questions.

| Steps | Time on one L40S | Before | After | Held-out set | Adapter |
|---|---|---|---|---|---|
| 30 | 55 min | 48.4% | **81.5%** | 184 questions, right answer | [Hub ↗](https://huggingface.co/cetusian/qwen3.5-2b-mcp-sqlite-grpo) |

## The task

The server is [mcp-server-sqlite](https://pypi.org/project/mcp-server-sqlite/), the
reference SQLite server, over the [Chinook](https://github.com/lerocha/chinook-database)
music store database (MIT): artists, albums, tracks, playlists, customers, invoices and
staff. The model sees only the server's three read tools (`list_tables`,
`describe_table`, `read_query`) as native tool calls, and has up to 8 turns. It never
sees the database schema or the SQL behind a question.

Questions come from 15 templates. Most need more than one lookup: "How long is the album
'Big Ones' in whole minutes, rounded down?", "How many Classical tracks are in the
playlist 'Classical 101 - Next Steps'?", "Which customer in Portugal has spent the most
in total?". One-step lookups the base model already gets right, such as a track's genre,
are kept but few. Each answer is computed from the database by a gold query, and a
question is dropped when it has no single answer (for example, a tie for "the most").
That gives 925 training questions and 184 held-out ones. The held-out set differs from
training in two ways:

- 20% of albums, artists and customers never appear in training, so a correct answer
  means the model looked it up rather than remembered it.
- 3 question types appear only in the held-out set, so it also tests phrasings and joins
  the model never trained on.

**Reward:** 1 when the answer is right, else 0: numbers to the cent, names ignoring case
and spacing. The prompt asks for `<answer>...</answer>`, and when the final reply has
one, only its text counts. A plain reply counts too, but only if it can't be hedging: a
name must appear as whole words in a reply of at most 300 characters, with no other
answer from the question set in it (names the question itself mentions are fine), and a
number must be the only number in the reply besides those in the question. So the reward measures finding the
answer, not the format, and listing guesses earns nothing. Logged but not rewarded:
whether the model used the tag, and how many tool calls it made.

## Can the base model do it?

About half the time. It knows how to call the tools, and when it answers, it's usually
right. It fails in two ways: it keeps exploring until its 8 turns run out, and on
multi-step questions it makes a slip in the SQL or the arithmetic (one answer divides
milliseconds by 60 and reports an album 3,600 minutes long).

## Run it

You need [uv](https://docs.astral.sh/uv/) on the path: the environment starts the MCP
server with `uvx`, pinned to the server's release date (it doesn't start against today's
MCP SDK). The database downloads on first use and is checked against its SHA-256.

```bash
CUDA_VISIBLE_DEVICES=0 surogate grpo-colocate \
  --train examples/kits/mcp/train.yaml \
  --infer examples/kits/mcp/infer.yaml \
  --orch  examples/kits/mcp/orch.yaml
```

16 questions × 8 rollouts a step, up to 8 turns of 512 tokens each, thinking off, LoRA
rank 16. Learning rate 5e-5 with cosine decay to a tenth by step 40. The run evaluates
before step 0 and every 10 steps, and keeps a checkpoint every 10 steps. About 55
minutes to the step-30 evaluation on one L40S, 71 minutes to step 40.

Every rollout talks to the same server process, so the server must be read-only or safe
to share. Here the model is shown only the three read tools, and `read_query` runs SELECT
statements only, so nothing can change the data under other rollouts.

## Measure again

| | Held-out right answer (184 questions) | No answer |
|---|---|---|
| Base Qwen3.5-2B | 48.4% | 0.0% |
| After 10 steps | 69.0% | 0.0% |
| After 20 steps | 78.8% | 1.1% |
| **After 30 steps** | **81.5%** | 0.5% |
| After 40 steps | 77.7% | 4.9% |

Greedy decoding, the same prompt and budget as training, one run. Step 30 is the one to
keep.

We checked the result outside the training run too: the model served on its own, with
the same 184 questions run through the environment. The base model scores 52.7% and the
step-30 adapter 89.1%. It fixed 67 answers and broke none. It also makes fewer tool calls
(4.7 a question against 5.8), and runs out of turns on 14 questions instead of 68.

## What changed

Mostly, it learned to stop exploring once it has what it needs, and answer:

> **Question:** How long is the album 'Big Ones' in whole minutes, rounded down?
>
> **Before:** 8 tool calls, out of turns, no answer.
>
> **After:** 4 tool calls. "The album 'Big Ones' is 73 minutes long."

> **Question:** Which artist released the album 'Bach: Toccata & Fugue in D Minor'?
>
> **Before:** 8 tool calls, out of turns, no answer.
>
> **After:** 4 tool calls. "The artist who released the album 'Bach: Toccata & Fugue in D Minor' is Ton Koopman."

And it makes fewer slips on the way:

> **Question:** How long is the album 'Faceless' in whole minutes, rounded down?
>
> **Before:** "216006 / 60 = 3600.1. Rounding down gives 3600 minutes."
>
> **After:** "The album 'Faceless' is 47 minutes long."

> **Question:** How many Classical tracks are in the playlist 'Classical 101 - Next Steps'?
>
> **Before:** "There are 25 Classical tracks in the playlist 'Classical 101 - Next Steps'." (the answer is 24)
>
> **After:** "There are 24 Classical tracks in the playlist 'Classical 101 - Next Steps'."

## Two lessons from building it

**Score the answer, not the format.** The first version paid only for an answer inside
`<answer>` tags. The base model scored 0%, which looked like a great before. But it was
finding the right answer most of the time, in a plain sentence, so the 0% measured the
tag format, and training mostly taught the tag. Accepting plain answers, with the guards
above so a list of guesses still earns nothing, gave an honest baseline of 48.4%.

The guards needed one more pass. The first plain-answer scorer only checked that the name
appeared somewhere in a short reply, so a short list of guesses, or "Rock And Roll" when
the answer is "Rock", still scored. A reviewer caught it. The scorer now requires the name
as whole words and no other answer next to it. We rescored the outside check's saved
replies with it: the base model lost one hedged answer (53.3% → 52.7%) and the trained
model lost none (89.1%). It had not learned to hedge, and the result stands.

**A reward that pays only the final answer can starve it.** That first version, with the
kits' usual constant learning rate of 1e-4, peaked at step 20 and then collapsed: the
model made more and more tool calls (5.0 a question at step 20, 6.8 at step 38) until it
ran out of turns before answering. Half the rate with cosine decay held. This kit uses
that schedule.

## Use the adapter

```python
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel

tok = AutoTokenizer.from_pretrained("Qwen/Qwen3.5-2B")
base = AutoModelForCausalLM.from_pretrained("Qwen/Qwen3.5-2B", torch_dtype="bfloat16")
model = PeftModel.from_pretrained(base, "cetusian/qwen3.5-2b-mcp-sqlite-grpo")
```

Pass the three tools with `tools=` in `apply_chat_template` and `enable_thinking=False`,
and run the tool loop yourself. With Surogate: `surogate serve Qwen/Qwen3.5-2B
--enable-auto-tool-choice` with the adapter loaded at runtime
([runtime adapters](/engine/inference/serving-models/)), and any MCP client in front.

## Use your own MCP server

The environment is one file plus its questions, in
[environments/mcp-sqlite](https://github.com/invergent-ai/surogate/blob/main/environments/mcp-sqlite/mcp_sqlite.py). To train
against your own server, change three things:

- `SERVER`: the command that starts your stdio MCP server.
- `READ_TOOLS`: the tools the model may call. Leave out anything that writes.
- `tasks.py`: your questions, each with an answer you can check.

Run `python mcp_sqlite.py` in the environment's directory first: it checks the scorer on
hand-written cases and that the server offers the tools you named.

## Limits

- One database and 15 question types. A new database needs new questions.
- A name must match as stored: a different spelling counts as wrong.
- One run, one seed. Expect a few points of variance between runs.

Next: compare the other [GRPO kits](/engine/examples/kits/), or read [RL Environments](/engine/guides/rl-environments/) to build a reward of your own.
