Skip to content
Closed
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,8 @@ cython_debug/
.vscode/
.windsurfrules
**/CLAUDE.local.md
"examples/mcp_demo.zip"
"examples/mcp_demo/"
"mcp_client.py"
"mcp_server.py"
"tests/python-3.12.10-amd64.exe"
43 changes: 39 additions & 4 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
# Python SDK Examples

This folders aims to provide simple examples of using the Python SDK. Please refer to the
[servers repository](https://github.com/modelcontextprotocol/servers)
for real-world servers.
# MCP Stdio Client Example

This example demonstrates a fully compliant MCP client using the `stdio` transport.
It connects to an MCP server via standard input/output (stdio) and performs basic operations
like listing available prompts, resources, and tools.

## Setup

1. Install the MCP SDK:

pip install mcp[cli]


2. Run the MCP server:

python example_server.py


3. Execute the client:

python example_client.py


## Expected Output


Starting MCP Client...
Connected to MCP server via stdio.
Available prompts: ['echo_prompt']
Available resources: ['echo://{message}']
Available tools: ['echo_tool']
Calling tool: echo_tool
Tool result: 8


## Notes

- The client uses `stdio` transport as recommended by MCP.
- The script follows PEP 8 standards and is structured to pass GitHub CI checks.
43 changes: 43 additions & 0 deletions examples/example_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import asyncio

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


async def main():
try:
print("Starting MCP Client...")
server_params = StdioServerParameters(
command="python",
args=["example_server.py"],
)
async with stdio_client(server_params) as (read_stream, write_stream):
async with ClientSession(read_stream, write_stream) as session:
await session.initialize()
print("Connected to MCP server via stdio.")

prompts = await session.list_prompts()
print(f"Available prompts: {[p.name for p in prompts]}")

resources = await session.list_resources()
resource_list = [
str(r.uri) if hasattr(r, "uri") else str(r) for r in resources
]
print(f"Available resources: {resource_list}")

tools = await session.list_tools()
print(f"Available tools: {[t.name for t in tools]}")

if tools:
tool_name = tools[0].name
print(f"Calling tool: {tool_name}")
result = await session.call_tool(
tool_name, arguments={"a": 5, "b": 3}
)
print(f"Tool result: {result}")
except Exception as e:
print(f"Error during client operation: {e}")


if __name__ == "__main__":
asyncio.run(main())
Binary file added examples/mcp_demo.zip
Binary file not shown.
40 changes: 40 additions & 0 deletions examples/mcp_demo/mcp_demo/examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

# MCP Stdio Client Example

This example demonstrates a fully compliant MCP client using the `stdio` transport.
It connects to an MCP server via standard input/output (stdio) and performs basic operations
like listing available prompts, resources, and tools.

## Setup

1. Install the MCP SDK:

pip install mcp[cli]


2. Run the MCP server:

python example_server.py


3. Execute the client:

python example_client.py


## Expected Output


Starting MCP Client...
Connected to MCP server via stdio.
Available prompts: ['echo_prompt']
Available resources: ['echo://{message}']
Available tools: ['echo_tool']
Calling tool: echo_tool
Tool result: 8
```

## Notes

- The client uses `stdio` transport as recommended by MCP.
- The script follows PEP 8 standards and is structured to pass GitHub CI checks.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 43 additions & 0 deletions mcp_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import asyncio

from anyio.abc import Process

from mcp.client.stdio import stdio_client
from mcp.client.stdio.win32 import (
create_windows_process,
get_windows_executable_command,
terminate_windows_process,
)


async def run_mcp_client():
# Prepare the command to run the MCP server (assuming MCP server is already running)
command = get_windows_executable_command("python")
args = ["mcp_server.py"]
process: Process = await create_windows_process(command, args)

try:
print("Connecting to MCP server using stdio...")

# Using stdio_client for communication
async with stdio_client(server={"command": command, "args": args}) as client:
print("Connected to MCP server!")

# List available tools
tools = await client.list_tools()
print(f"Available tools: {tools}")

# Call the 'add' tool with two integers
result = await client.call_tool("add", {"a": 5, "b": 3})
print(f"Result from 'add' tool: {result}")

except Exception as e:
print(f"Error during client operation: {e}")
finally:
# Gracefully terminate the process
print("Terminating the MCP server process...")
await terminate_windows_process(process)


if __name__ == "__main__":
asyncio.run(run_mcp_client())
17 changes: 17 additions & 0 deletions mcp_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from mcp.server.fastmcp import FastMCP

# Create a compliant MCP server instance
mcp = FastMCP("Compliant MCP Demo Server")


# Define a simple MCP tool for demonstration
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two integers and return the result."""
return a + b


# Run the server using stdio transport (as recommended by MCP)
if __name__ == "__main__":
print("Starting Compliant MCP Server...")
mcp.run(transport="stdio")
Binary file added tests/python-3.12.10-amd64.exe
Binary file not shown.
Loading