Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated example in README.md for increased outreach #82

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 33 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

**example_server.py**

```python
# /// script
# dependencies = [
Expand Down Expand Up @@ -150,39 +152,52 @@ if __name__ == "__main__":

### Creating a Client

**example_client.py**

```python
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client

# Create server parameters for stdio connection
server_params = StdioServerParameters(
command="path/to/server",
args=[], # Optional command line arguments
command="python", # Executable
args=["example_server.py"], # Optional command line arguments
env=None # Optional environment variables
)

async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
# Initialize the connection
await session.initialize()
async def run():
async with stdio_client(server_params) as (read, write):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

meanwhile I'm just trying to figure out how to launch a client that doesn't require launching a server in the same script lol

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like anyio supports tcp (connect_tcp). One option is to follow that route, having the server listen in a port and the client connect to it. That's probably the idea moving forward when running client/servers from different hosts (better if upgraded to TLS).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async with ClientSession(read, write) as session:
# Initialize the connection
await session.initialize()

# The example server only supports prompt primitives:

# List available prompts
prompts = await session.list_prompts()

# List available resources
resources = await session.list_resources()
# Get a prompt
prompt = await session.get_prompt("example-prompt", arguments={"arg1": "value"})

# List available prompts
prompts = await session.list_prompts()
"""
Other example calls include:

# List available tools
tools = await session.list_tools()
# List available resources
resources = await session.list_resources()

# Read a resource
resource = await session.read_resource("file://some/path")
# List available tools
tools = await session.list_tools()

# Call a tool
result = await session.call_tool("tool-name", arguments={"arg1": "value"})
# Read a resource
resource = await session.read_resource("file://some/path")

# Get a prompt
prompt = await session.get_prompt("prompt-name", arguments={"arg1": "value"})
# Call a tool
result = await session.call_tool("tool-name", arguments={"arg1": "value"})
"""

if __name__ == "__main__":
import asyncio
asyncio.run(run())
```

## Primitives
Expand Down