Merge pull request #82 from miguelg719/80

Updated example in README.md for increased outreach
This commit is contained in:
David Soria Parra 2024-12-02 11:39:31 +00:00 • committed by GitHub
commit c40009524c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -75,6 +75,8 @@ 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. 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.
**example_server.py**
```python ```python
# /// script # /// script
# dependencies = [ # dependencies = [
@ -150,39 +152,52 @@ if __name__ == "__main__":
### Creating a Client ### Creating a Client
**example_client.py**
```python ```python
from mcp import ClientSession, StdioServerParameters from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client from mcp.client.stdio import stdio_client
# Create server parameters for stdio connection # Create server parameters for stdio connection
server_params = StdioServerParameters( server_params = StdioServerParameters(
command="path/to/server", command="python", # Executable
args=[], # Optional command line arguments args=["example_server.py"], # Optional command line arguments
env=None # Optional environment variables env=None # Optional environment variables
) )
async with stdio_client(server_params) as (read, write): async def run():
async with ClientSession(read, write) as session: async with stdio_client(server_params) as (read, write):
# Initialize the connection async with ClientSession(read, write) as session:
await session.initialize() # Initialize the connection
await session.initialize()
# List available resources # The example server only supports prompt primitives:
resources = await session.list_resources()
# List available prompts # List available prompts
prompts = await session.list_prompts() prompts = await session.list_prompts()
# List available tools # Get a prompt
tools = await session.list_tools() prompt = await session.get_prompt("example-prompt", arguments={"arg1": "value"})
# Read a resource """
resource = await session.read_resource("file://some/path") Other example calls include:
# Call a tool # List available resources
result = await session.call_tool("tool-name", arguments={"arg1": "value"}) resources = await session.list_resources()
# Get a prompt # List available tools
prompt = await session.get_prompt("prompt-name", arguments={"arg1": "value"}) tools = await session.list_tools()
# Read a resource
resource = await session.read_resource("file://some/path")
# Call a tool
result = await session.call_tool("tool-name", arguments={"arg1": "value"})
"""
if __name__ == "__main__":
import asyncio
asyncio.run(run())
``` ```
## Primitives ## Primitives