OpenAI Agents SDK · Chapter 15 of 24

MCP

By the end of this chapter you can say what MCP is and why Shikhar's teams would use it, write an MCP server with the mcp SDK, connect an agent to it with MCPServerStdio, list a server's tools, give one agent two servers with only the tools it should see, and say when a tool belongs behind a server and when it is better as a function tool. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Memory
  • 4 tasks
  • Python
  • In the all labs plan

The first 2 chapters of OpenAI Agents SDK are free. This one comes with the all labs plan.

Why you need this

So far every tool lived in the helpdesk's own file. But Shikhar's systems belong to different people: the timetable to the academic office, the hostel register to Bhanu, fees and refunds to Nidhi. Each team wants to write its tools once, keep them in its own program, and have the helpdesk agent use them, and also the other assistants Shikhar is starting to use: a coding assistant in the academic office, a desktop chat app on Nidhi's laptop. Copying fee_status into every one of them, and keeping the copies the same, does not last.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    hostel_server.py: Shikhar's hostel as an MCP server

    Write an MCP server with the mcp SDK: an MCPServer named shikhar-hostel, with instructions (a sentence for the model on what the server is for), and two tools with type hints and docstrings: find_hostel_beds(gender), free beds for boys (male) or girls (female), returning the JSON text of shikhar.find_hostel_beds(gender); and book_hostel_bed(student_id, bed_id), a room change, returning the JSON text of shikhar.book_hostel_bed(student_id, bed_id).

    End the file by running the server under if __name__ == "__main__":. It prints nothing. The check starts it over stdio, like any client, lists its tools and calls both.

  2. Task 2

    hostel_agent.py: connect, list, and answer

    Connect to hostel_server.py with the Agents SDK's MCPServerStdio (command python3, the file's name as the argument, and env with PYTHONPATH set to /opt/track/lib), as an async context manager. Print one line per tool the server lists: its name, a colon and a space, then its description.

    Then make an agent for lab-chat with the instructions You are Shikhar Classes' hostel desk. Use the tools. whose tools come only from the server, run it on Girls hostel me koi bed khali hai? and print the run's final output.

  3. Task 3

    two_servers.py: two servers, filtered

    fees_server.py is Nidhi's fees server (do not change it): fee_status, refund_quote and request_refund. Make ONE agent for lab-chat with the instructions You are Shikhar Classes' helpdesk. Use the tools. and two MCP servers: fees_server.py, with a tool filter that allows only fee_status and refund_quote, and hostel_server.py. Cache both servers' tool lists.

    Run the agent on Is any instalment pending for S2041? then on Boys hostel me koi bed khali hai?, and print each run's final output on its own line.

  4. Task 4 · interview question

    Interview: mcp_or_function.py, MCP tools or function tools

    Make two agents for lab-chat, both with the instructions You are Shikhar Classes' hostel desk. Use the tools.: a plain one with one function tool of its own, find_hostel_beds(gender), written in this file as in chapter 4 (the JSON text of shikhar.find_hostel_beds(gender)); and an MCP one with the tools of hostel_server.py.

    Ask both Is any bed free in the girls hostel?, the plain agent first, and print function: and its final output, then mcp: and the MCP agent's final output.

    The interviewer will ask: the model saw the same tool both times. When would you put Shikhar's tools behind an MCP server, and when keep them as function tools?

What you will learn

  • MCP in one paragraph: servers, clients and the tools between them.
  • A server with the mcp SDK: MCPServer and @mcp.tool().
  • MCPServerStdio: starting a server and connecting the agent to it.
  • list_tools, tool_filter and cache_tools_list.
  • MCP tools or function tools, and why.

More in Memory

Build "MCP" yourself

Shikhar Classes, a Kota coaching institute, answers parents and students on WhatsApp all day. You build its helpdesk assistant. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of OpenAI Agents SDK are free. This one comes with the all labs plan.