Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 7.8 — The Model Context Protocol in Brief
LLM Engineering · Example 1 of 1

A tools/list and tools/call exchange with a fake server

A sketch of the JSON-RPC shape, not a real MCP implementation. Real messages carry more fields: in revision 2026-07-28 every request also has a _meta with the protocol version, client info and client capabilities (earlier revisions started with an initialize handshake instead), and results add a resultType such as "complete". The tool and result shapes follow the specification's examples.

The Model Context Protocol (MCP) is an open standard that lets an AI application connect to tool servers through one common JSON-RPC protocol, instead of writing custom glue for every tool. It also covers resources and prompts. The same safety rules still apply.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write make_request(id, method, params) that returns the compact JSON text of a JSON-RPC 2.0 request with keys in the order jsonrpc, id, method, params.

02

Write to_model_tool(mcp_tool) that converts an MCP tool dict (name, description, inputSchema) into the model's function-tool dict.

Explain it without notes

01

What problem does MCP solve and what are the roles in it?

02

What do tools/list and tools/call do, and how are tool errors reported?

03

Which safety responsibilities remain with the application when it uses MCP servers?

04

Your team can expose its tools through plain function calling or through an MCP server. How do you choose?

A tools/list and tools/call exchange with a fake server
Sign in to run this example in your browser.

Expected output

>> {"jsonrpc":"2.0","id":1,"method":"tools/list","params":{}}
<< {"jsonrpc":"2.0","id":1,"result":{"tools":[{"name":"get_weather","description":"Get the current weather for a city.","inputSchema":{"type":"object","properties":{"city":{"type":"string"}},"required":["city"]}}]}}
tools for the model: get_weather
>> {"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"get_weather","arguments":{"city":"Pune"}}}
<< {"jsonrpc":"2.0","id":2,"result":{"content":[{"type":"text","text":"Pune: 31 C"}],"isError":false}}
call 2 -> ok: Pune: 31 C
>> {"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"get_weather","arguments":{"city":"Punee"}}}
<< {"jsonrpc":"2.0","id":3,"result":{"content":[{"type":"text","text":"unknown city: Punee"}],"isError":true}}
call 3 -> error: unknown city: Punee
>> {"jsonrpc":"2.0","id":4,"method":"resources/list","params":{}}
<< {"jsonrpc":"2.0","id":4,"error":{"code":-32601,"message":"Method not found"}}
call 4 -> error -32601 Method not found