From 569acf077d4b70b2fb8ce8ed4b52dbed0ac54c4c Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Mon, 25 Nov 2024 08:44:19 -0800 Subject: [PATCH] Fix first Python demo so it works You can't do async with ... outside of an async def function --- README.md | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 01b702c..4937e7b 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,11 @@ Connections between clients and servers are established through transports like MCP servers follow a decorator approach to register handlers for MCP primitives like resources, prompts, and tools. The goal is to provide a simple interface for exposing capabilities to LLM clients. ```python +# /// script +# dependencies = [ +# "mcp" +# ] +# /// from mcp.server import Server, NotificationOptions from mcp.server.models import InitializationOptions import mcp.server.stdio @@ -122,20 +127,25 @@ async def handle_get_prompt( ] ) -# Run the server as STDIO -async with mcp.server.stdio.stdio_server() as (read_stream, write_stream): - await server.run( - read_stream, - write_stream, - InitializationOptions( - server_name="example", - server_version="0.1.0", - capabilities=server.get_capabilities( - notification_options=NotificationOptions(), - experimental_capabilities={}, +async def run(): + # Run the server as STDIO + async with mcp.server.stdio.stdio_server() as (read_stream, write_stream): + await server.run( + read_stream, + write_stream, + InitializationOptions( + server_name="example", + server_version="0.1.0", + capabilities=server.get_capabilities( + notification_options=NotificationOptions(), + experimental_capabilities={}, + ) ) ) - ) + +if __name__ == "__main__": + import asyncio + asyncio.run(run()) ``` ### Creating a Client