|
| 1 | +""" |
| 2 | +Minimal example showing how to interact with an MCP server using Python. |
| 3 | +
|
| 4 | +This script demonstrates: |
| 5 | +1. Listing available tools |
| 6 | +2. Calling a specific tool |
| 7 | +
|
| 8 | +Requirements: |
| 9 | + pip install mcp |
| 10 | +""" |
| 11 | + |
| 12 | +import asyncio |
| 13 | +import json |
| 14 | +from typing import Any, List |
| 15 | + |
| 16 | +from mcp.client.session import ClientSession |
| 17 | +from mcp.client.streamable_http import streamablehttp_client |
| 18 | +from mcp.types import CallToolResult, Tool |
| 19 | + |
| 20 | +# --------------------------------------------------------------------- |
| 21 | +# Configuration |
| 22 | +# --------------------------------------------------------------------- |
| 23 | + |
| 24 | +SERVER_URL = "http://localhost:8000/mcp" # Update to your MCP server URL |
| 25 | + |
| 26 | + |
| 27 | +# --------------------------------------------------------------------- |
| 28 | +# MCP helpers |
| 29 | +# --------------------------------------------------------------------- |
| 30 | + |
| 31 | + |
| 32 | +async def list_tools() -> List[Tool]: |
| 33 | + """ |
| 34 | + Fetch and return the list of tools exposed by the MCP server. |
| 35 | + """ |
| 36 | + async with streamablehttp_client(url=SERVER_URL) as ( |
| 37 | + read_stream, |
| 38 | + write_stream, |
| 39 | + _, |
| 40 | + ): |
| 41 | + async with ClientSession(read_stream, write_stream) as session: |
| 42 | + await session.initialize() |
| 43 | + return (await session.list_tools()).tools |
| 44 | + |
| 45 | + |
| 46 | +async def call_tool( |
| 47 | + tool_name: str, |
| 48 | + arguments: dict[str, Any] | None = None, |
| 49 | +) -> CallToolResult: |
| 50 | + """ |
| 51 | + Call a tool by name with optional arguments. |
| 52 | + """ |
| 53 | + async with streamablehttp_client(url=SERVER_URL) as ( |
| 54 | + read_stream, |
| 55 | + write_stream, |
| 56 | + _, |
| 57 | + ): |
| 58 | + async with ClientSession(read_stream, write_stream) as session: |
| 59 | + await session.initialize() |
| 60 | + return await session.call_tool(tool_name, arguments) |
| 61 | + |
| 62 | + |
| 63 | +# --------------------------------------------------------------------- |
| 64 | +# Example usage |
| 65 | +# --------------------------------------------------------------------- |
| 66 | + |
| 67 | + |
| 68 | +async def main(): |
| 69 | + print(f"Connecting to MCP server at {SERVER_URL} ...") |
| 70 | + # List available tools |
| 71 | + tools = await list_tools() |
| 72 | + print(f"{len(tools)} tools found") |
| 73 | + SEP = "-" * 80 |
| 74 | + for tool in tools: |
| 75 | + output = f""" |
| 76 | +TOOL: {tool.name} |
| 77 | +{SEP} |
| 78 | +Description: |
| 79 | +{tool.description.strip()} |
| 80 | +
|
| 81 | +{SEP} |
| 82 | +Input Schema: |
| 83 | +{json.dumps(tool.inputSchema, indent=2)} |
| 84 | +
|
| 85 | +{SEP} |
| 86 | +Output Schema: |
| 87 | +{json.dumps(tool.outputSchema, indent=2)} |
| 88 | +{SEP} |
| 89 | +{SEP} |
| 90 | +""".strip() |
| 91 | + print(output) |
| 92 | + |
| 93 | + # Example: call a tool |
| 94 | + # Uncomment and update as needed |
| 95 | + # |
| 96 | + # result = await call_tool( |
| 97 | + # tool_name="health", # Replace with the desired tool name |
| 98 | + # arguments={}, # Replace with the desired arguments |
| 99 | + # ) |
| 100 | + # |
| 101 | + # print("\nTool result:") |
| 102 | + # print(result) |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == "__main__": |
| 106 | + asyncio.run(main()) |
0 commit comments