|
1 | 1 | # mcp-python
|
2 |
| -Model Context Protocol implementation for Python |
| 2 | + |
| 3 | +Python implementation of the Model Context Protocol (MCP), providing both client and server capabilities for integrating with LLM surfaces. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The Model Context Protocol allows applications to provide context for LLMs in a standardized way, separating the concerns of providing context from the actual LLM interaction. This Python SDK implements the full MCP specification, making it easy to: |
| 8 | + |
| 9 | +- Build MCP clients that can connect to any MCP server |
| 10 | +- Create MCP servers that expose resources, prompts and tools |
| 11 | +- Use standard transports like stdio and SSE |
| 12 | +- Handle all MCP protocol messages and lifecycle events |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +```bash |
| 17 | +uv add mcp-python |
| 18 | +``` |
| 19 | + |
| 20 | +## Quick Start |
| 21 | + |
| 22 | +### Creating a Client |
| 23 | + |
| 24 | +```python |
| 25 | +from mcp_python import ClientSession |
| 26 | +from mcp_python.client.stdio import stdio_client |
| 27 | + |
| 28 | +async with stdio_client(command="path/to/server") as (read, write): |
| 29 | + async with ClientSession(read, write) as session: |
| 30 | + # Initialize the connection |
| 31 | + await session.initialize() |
| 32 | + |
| 33 | + # List available resources |
| 34 | + resources = await session.list_resources() |
| 35 | +``` |
| 36 | + |
| 37 | +### Creating a Server |
| 38 | + |
| 39 | +```python |
| 40 | +from mcp_python.server import Server |
| 41 | +from mcp_python.server.stdio import stdio_server |
| 42 | + |
| 43 | +# Create a server instance |
| 44 | +server = Server("example-server") |
| 45 | + |
| 46 | +# Add capabilities |
| 47 | +@server.list_resources() |
| 48 | +async def list_resources(): |
| 49 | + return [ |
| 50 | + { |
| 51 | + "uri": "file:///example.txt", |
| 52 | + "name": "Example Resource" |
| 53 | + } |
| 54 | + ] |
| 55 | + |
| 56 | +# Run the server |
| 57 | +async with stdio_server() as (read, write): |
| 58 | + await server.run(read, write, server.create_initialization_options()) |
| 59 | +``` |
| 60 | + |
| 61 | +## Documentation |
| 62 | + |
| 63 | +- [MCP Specification](https://modelcontextprotocol.github.io) |
| 64 | +- [Example Servers](https://github.com/modelcontextprotocol/example-servers) |
| 65 | + |
| 66 | +## Contributing |
| 67 | + |
| 68 | +Issues and pull requests are welcome on GitHub at https://github.com/modelcontextprotocol/python-sdk. |
| 69 | + |
| 70 | +## License |
| 71 | + |
| 72 | +This project is licensed under the MIT License - see the LICENSE file for details. |
0 commit comments