# MCP Agent Fine-tuning Workshop

Ronan McGovern, Trellis Research | AI Engineer World's Fair 2025 | 35:30

Source: https://www.youtube.com/watch?v=Nqb7JTx0Pqo
Channel: AI Engineer (https://www.youtube.com/@aiDotEngineer). Summarised by AIE Talks.
Page: https://aietalks.com/talks/mcp-agent-fine-tuning-workshop
Published: 2025-06-03
Tags: fine-tuning, mcp, synthetic-data, tool-use

## TL;DR
- MCP lets a language model discover and call tools through a service, but an OpenAI-style agent must translate tool schemas, responses, and emitted tool calls between formats.
- High-quality traces should include the tool definitions and complete multi-turn message history, then be cleaned before uploading as a fine-tuning dataset.
- A small Qwen3 model can be fine-tuned with LoRA on these traces, although meaningful evaluation needs more data, an evaluation split, and tests on multi-step tasks.

## Summary
Ronan McGovern demonstrates an end-to-end workflow for fine-tuning an MCP-capable agent. The example uses a Playwright browser MCP server, an OpenAI-compatible endpoint running Qwen3, and an agent that logs tool definitions, reasoning, tool calls, tool responses, and final answers. McGovern shows how to inspect and manually clean traces, upload them to Hugging Face, and unroll multi-turn conversations into several training rows. The fine-tuning notebook uses Unsloth, a 4-billion-parameter Qwen3 model, and LoRA adapters applied to attention and MLP layers. The workshop uses only nine examples, so its evaluation is deliberately limited. McGovern is direct about the model's weaknesses on multi-step browser work and recommends collecting hundreds of traces for a serious experiment. He also argues that supervised fine-tuning on curated traces is a useful starting point before applying reward-based methods such as GRPO.

## Key ideas
### MCP connects a model to tools through discovery and execution
[01:03](https://www.youtube.com/watch?v=Nqb7JTx0Pqo&t=63s)
McGovern defines MCP, or Model Context Protocol, as a way to provide language models with services such as tool access. The MCP service stores information about available tools so the model can decide how to call them, then runs the selected tool and returns its result. The workshop uses browser control through Playwright, but the same pattern could include Stripe, GitHub, Gmail, or other MCP servers. The model can loop between reasoning, tool calls, returned page information, and a final text response.

### An OpenAI-style endpoint requires translation in both directions
[02:25](https://www.youtube.com/watch?v=Nqb7JTx0Pqo&t=145s)
The workshop exposes the language model through an OpenAI-compatible API because many libraries expect that interface. MCP tool descriptions must be converted into JSON tool definitions for the endpoint. Tool responses must also be converted into the format the model expects. The agent then has to detect when the model's emitted text is a tool call and extract it into JSON. In this example, the Qwen model emits calls in Hermes format, which the tool parser converts before sending them to MCP.

### The prompt teaches the model how to emit tool calls
[03:41](https://www.youtube.com/watch?v=Nqb7JTx0Pqo&t=221s)
The pseudo prompt begins with a system message that explains the tool-call format. The model is told to return each function call as JSON inside tool-call XML tags. A user request follows, such as navigating to a website. The assistant may think, emit a browser call, receive the resulting accessibility tree, and then continue thinking or answer directly. This structure is inserted into the actual chat template used by the agent and later by the fine-tuning code.

### A compatible data-generation model can expose reasoning traces
[05:40](https://www.youtube.com/watch?v=Nqb7JTx0Pqo&t=340s)
For data collection, McGovern recommends keeping the model that generates traces reasonably consistent with the model being fine-tuned. He uses a Qwen3 mixture-of-experts model with 30 billion total parameters and 3 billion activated parameters, hosted on RunPod. The endpoint enables reasoning and a reasoning parser, so think tokens are extracted into a separate reasoning field. It also enables automatic tool choice and a Hermes tool parser. OpenAI models are not used because they do not share their thinking traces.

### Useful training records contain tools and the full conversation
[12:11](https://www.youtube.com/watch?v=Nqb7JTx0Pqo&t=731s)
Each run is logged with a messages part and a tools part. The tools record contains the browser functions, while messages preserve the user request, assistant reasoning, tool call, tool response, and final answer. Browser navigation returns an accessibility tree, which can be very long, so the example truncates the response to its top section. McGovern warns that naive truncation can hide information from the model and would need a more careful implementation for deeper work.

### Trace quality improves through manual cleanup and prompting
[15:01](https://www.youtube.com/watch?v=Nqb7JTx0Pqo&t=901s)
Browser tasks can produce noisy traces even when the final answer is correct. McGovern suggests manually editing an awkward exchange, deleting redundant user turns, or combining instructions. A system prompt can also give very direct guidance about which steps and tools to use. Once a clean trace has been produced, that temporary system prompt does not need to be included in the training data. The goal is to preserve a tidy example of the desired behavior rather than every imperfect interaction.

### Unrolling multi-turn traces creates several training rows
[18:09](https://www.youtube.com/watch?v=Nqb7JTx0Pqo&t=1089s)
The dataset push function can unroll a conversation into rows for different prefixes of the interaction. A three-turn exchange becomes one row containing all three turns, another containing two, and another containing one. McGovern reports that four saved traces become nine examples in his demonstration. This matters because the Qwen chat template includes reasoning from the most recent turn. Keeping only one full conversation would therefore provide less useful coverage of earlier interaction states.

### LoRA makes a small fine-tuning run possible on limited hardware
[22:47](https://www.youtube.com/watch?v=Nqb7JTx0Pqo&t=1367s)
The notebook trains a 4-billion-parameter Qwen3 model with Unsloth. Instead of updating every model weight, it attaches low-rank adapters to attention modules and MLP layers. The example uses rank 32 and a rescaled LoRA configuration. With nine rows, batch size one, gradient accumulation of one, and one epoch, only 1.62% of the parameters are trained. The main weights remain frozen, which reduces memory use, though the resulting loss is noisy because the dataset and batch are small.

### Curated supervised traces should precede reward-based training
[32:08](https://www.youtube.com/watch?v=Nqb7JTx0Pqo&t=1928s)
McGovern recommends starting with manually curated traces before trying reinforcement learning. Without supervised fine-tuning in the target domain, a model may generate many rollouts and rarely reach a correct answer that earns a positive reward. A GRPO setup also needs a verifiable dataset and reward definition. His browser example would require generating tasks with known answers, such as asking the model to find text on obscure pages and rewarding it when the extracted text matches the ground truth.

## Notable quotes
- "MCP does a few things. It's first of all a store of information on tools." (01:41)
- "So, this is a very nice trace and we're going to keep it." (12:49)
- "The goal is to get nice traces for training data." (16:16)
- "My recommendation there is start off with manual traces and curate them like this." (32:08)
- "If you've got a small number of examples, I think you can get significant improvements in performance." (34:48)

## Tools & references mentioned
- Trellis Research
- AI Worlds Fair 2025
- Model Context Protocol (MCP)
- Playwright
- Stripe
- GitHub
- Gmail
- OpenAI
- Qwen3
- RunPod
- LM Studio
- Hugging Face Hub
- Unsloth
- LoRA
- GRPO
- Windsurf
- UV
- TensorBoard

## Who should watch
- You are building an agent that needs to call MCP tools and want an OpenAI-compatible integration pattern.
- You have successful agent runs but need to turn their tool calls and conversations into training data for a smaller model.
- You are considering GRPO or another reward-based method and want to understand why curated supervised traces may be a better starting point.

## Related talks

- [MCP is all you need](https://aietalks.com/talks/mcp-is-all-you-need) (Samuel Colvin, Pydantic, 15:24)
- [Just do it. (let your tools think for themselves)](https://aietalks.com/talks/just-do-it-let-your-tools-think-for-themselves) (Robert Chandler, Wordware, 06:50)
- [Self-Training Agents: Hermes Agent, HF Traces, Skills, MCP & Finetuning](https://aietalks.com/talks/self-training-agents-hermes-agent-hf-traces-skills-mcp-finetuning) (Merve Noyan, Hugging Face, 19:11)
- [Will Agent Evaluation via MCP Stabilize Agent Networks?](https://aietalks.com/talks/will-agent-evaluation-via-mcp-stabilize-agent-networks) (Ari Heljakka, Root Signals, 14:11)
- [Agent Reinforcement Fine-Tuning](https://aietalks.com/talks/agent-reinforcement-fine-tuning) (Will Hang & Cathy Zhou, OpenAI, 16:55)
