SOLFIND
Web Lens
Portal home

MCP Python SDK - MCP Python SDK

https://py.sdk.modelcontextprotocol.io/ • 54 KB fetched
Open original page


MCP Python SDK - MCP Python SDK Skip to content MCP Python SDK MCP Python SDK * en - English * de - Deutsch * es - español * fr - français * hi - हिन्दी * ja - 日本語 * ko - 한국어 * pt - português (Brasil) * ru - русский язык * tr - Türkçe * uk - українська мова * zh - 简体中文 * zh-hant - 繁體中文 Search modelcontextprotocol/python-sdk MCP Python SDK modelcontextprotocol/python-sdk * MCP Python SDK MCP Python SDK On this page * Requirements * Installation * Example * Create it * Run it * Try it * Recap * Where to go next * What's new in v2 * Get started Get started * Installation * First steps * Connect to a real host * Testing * Servers Servers * Tools * Structured Output * Resources * URI templates * Prompts * Completions * Images, audio & icons * Handling errors * Inside your handler Inside your handler * The Context * Dependencies * Lifespan * Elicitation * Multi-round-trip requests * Sampling and roots * Progress * Logging * Subscriptions * Running your server Running your server * Add to an existing app * Deploy & scale * Authorization * OpenTelemetry * Serving legacy clients * Clients Clients * Callbacks * Transports * OAuth * Identity assertion * Multiple servers * Subscriptions * Caching * Protocol versions * Deprecated features * Advanced Advanced * The low-level Server * Pagination * Middleware * Extensions * MCP Apps * Troubleshooting * Translations * Migration Guide * API Reference API Reference * mcp * mcp_types On this page * Requirements * Installation * Example * Create it * Run it * Try it * Recap * Where to go next MCP Python SDK This documents v2, the current stable release line New to v2, or coming from v1? What's new in v2 is the five-minute tour of what changed, and the Migration Guide covers every breaking change. Still on v1.x? Its documentation lives at the v1.x docs . Something rough or confusing? Tell us . The Model Context Protocol (MCP) lets applications provide context to LLMs in a standardized way, separating the concern of providing context from the LLM interaction itself. This is the official Python SDK for it. With it you can: * Build MCP servers that expose tools, resources, and prompts to any MCP host. * Build MCP clients that connect to any MCP server. * Speak every standard transport: stdio, Streamable HTTP, and SSE. Requirements Python 3.10+. Installation uv pip uv add "mcp[cli]" pip install "mcp[cli]" The [cli] extra gives you the mcp command; you'll want it for development. See Installation for what each dependency is for. Example Create it Create a file server.py : server.py from mcp.server import MCPServer mcp = MCPServer ( "Demo" ) @mcp . tool () def add ( a : int , b : int ) -> int : """Add two numbers.""" return a + b @mcp . resource ( "greeting:// {name} " ) def greeting ( name : str ) -> str : """Greet someone by name.""" return f "Hello, { name } !" That's a complete MCP server. It exposes one tool , add , and one templated resource , greeting://{name} . Run it uv run mcp dev server.py This starts your server and opens the MCP Inspector , an interactive UI for poking at it. Open the URL it prints. Note The Inspector is a Node.js app, so mcp dev needs npx on your PATH . Try it In the Inspector, go to Tools and call add with a=1 , b=2 . You get 3 back. ✨ The Inspector built that form (a required integer field for a , another for b ) from your type hints. So will Claude, and every other MCP host. Now go to Resources and read greeting://World : Hello, World! Recap Look again at what you did not write: * No JSON Schema. a: int, b: int is the schema. * No request parsing, no serialization, no validation code. * No protocol handling at all. You wrote two Python functions with type hints and a docstring. The SDK does the rest. Where to go next * Get started takes you from install to a working, tested server. * Building an application that uses MCP servers? Start with Clients . * Already have a FastAPI or Starlette app? Add to an existing app mounts an MCP server inside it. * Hunting an exact error message? Troubleshooting is keyed by the verbatim text. * Wondering what changed in v2? What's new in v2 is the five-minute tour. * Migrating from v1? Start with the Migration Guide . * Hunting for an exact signature? The API Reference is generated from the source. * Reading with an LLM? This documentation is also published in the llms.txt format: llms.txt is an index of the pages, and llms-full.txt contains every page in a single file. Back to top Next What's new in v2 Made with Zensical

Links found on this page

  1. Skip to content [direct]
  2. de - Deutsch [direct]
  3. es - español [direct]
  4. fr - français [direct]
  5. hi - हिन्दी [direct]
  6. ja - 日本語 [direct]
  7. ko - 한국어 [direct]
  8. pt - português (Brasil) [direct]
  9. ru - русский язык [direct]
  10. tr - Türkçe [direct]
  11. uk - українська мова [direct]
  12. zh - 简体中文 [direct]
  13. zh-hant - 繁體中文 [direct]
  14. modelcontextprotocol/python-sdk [direct]
  15. What's new in v2 [direct]
  16. Get started [direct]
  17. Installation [direct]
  18. First steps [direct]
  19. Connect to a real host [direct]
  20. Testing [direct]
  21. Servers [direct]
  22. Tools [direct]
  23. Structured Output [direct]
  24. Resources [direct]
  25. URI templates [direct]
  26. Prompts [direct]
  27. Completions [direct]
  28. Images, audio & icons [direct]
  29. Handling errors [direct]
  30. Inside your handler [direct]
  31. The Context [direct]
  32. Dependencies [direct]
  33. Lifespan [direct]
  34. Elicitation [direct]
  35. Multi-round-trip requests [direct]
  36. Sampling and roots [direct]
  37. Progress [direct]
  38. Logging [direct]
  39. Subscriptions [direct]
  40. Running your server [direct]
  41. Add to an existing app [direct]
  42. Deploy & scale [direct]
  43. Authorization [direct]
  44. OpenTelemetry [direct]
  45. Serving legacy clients [direct]
  46. Clients [direct]
  47. Callbacks [direct]
  48. Transports [direct]
  49. OAuth [direct]
  50. Identity assertion [direct]
  51. Multiple servers [direct]
  52. Subscriptions [direct]
  53. Caching [direct]
  54. Protocol versions [direct]
  55. Deprecated features [direct]
  56. Advanced [direct]
  57. The low-level Server [direct]
  58. Pagination [direct]
  59. Middleware [direct]
  60. Extensions [direct]
  61. MCP Apps [direct]
  62. Troubleshooting [direct]
  63. Translations [direct]
  64. Migration Guide [direct]
  65. mcp [direct]
  66. mcp_types [direct]
  67. v1.x docs [direct]
  68. Tell us [direct]
  69. MCP Inspector [direct]
  70. llms.txt [direct]
  71. llms.txt [direct]
  72. llms-full.txt [direct]
  73. Zensical [direct]