Skip to main content

A Python client for interacting with MCP servers via Server-Sent Events and JSON-RPC

Project description

MCPClient Python Library Overview The MCPClient is a Python client for interacting with an MCP (Message Control Protocol) server via Server-Sent Events (SSE) and JSON-RPC. It provides functionality to connect to an MCP server, initialize a session, list available tools, and execute tools interactively or programmatically. Features

Connects to an MCP server using SSE Supports JSON-RPC 2.0 for sending requests and notifications Automatically discovers and parses available tools and their schemas Provides an interactive command-line interface for running tools Validates required parameters before tool execution Handles both required and optional parameters with type checking

Installation No external dependencies are required beyond the Python standard library (json, urllib.request, urllib.parse). To use the library, save the provided Python code as py_mcp_client.py and import it into your project. Usage Basic Example from py_mcp_client import MCPClient

Initialize client with SSE URL

client = MCPClient("https://example.com/sse")

try: # Connect to the server client.connect()

# Initialize session and get available tools
init_response, tools = client.initialize()
print(f"Connected to server: {init_response['result']['serverInfo']['name']}")
print(f"Available tools: {list(tools.keys())}")

# Run a tool programmatically
result = client.run_tool("example_tool", {"param1": "value1"})
print(result)

# Start interactive mode
client.interactive_run()

except Exception as e: print(f"Error: {e}")

Command-Line Usage Run the script directly and provide the SSE URL as a command-line argument or input it when prompted: python py_mcp_client.py https://example.com/sse

The script will:

Connect to the specified SSE URL Initialize the session Display server information and available tools Enter an interactive mode where you can select and run tools

Interactive Mode In interactive mode, the client:

Lists all available tools with their descriptions Allows selection of a tool by number Prompts for required and optional parameters Executes the selected tool and displays the result Continues until you choose to exit (option 0)

Methods

connect(): Establishes a connection to the SSE endpoint and retrieves the message URL. send_request(method, params): Sends a JSON-RPC request with an ID and returns the request ID. send_notification(method, params): Sends a JSON-RPC notification without an ID. initialize(): Initializes the MCP session and retrieves available tools. run_tool(tool_name, arguments): Executes a specified tool with provided arguments. interactive_run(): Starts the interactive command-line interface.

Error Handling

The client validates required parameters before tool execution. It handles various response formats (JSON and plain strings) for robustness. Errors during connection, initialization, or tool execution are caught and displayed.

Limitations

Requires a stable internet connection for SSE communication. Assumes the server follows the MCP protocol with JSON-RPC 2.0. Limited to the capabilities provided by the server's toolset.

Contributing Feel free to fork the repository, make improvements, and submit pull requests. For issues or feature requests, please open an issue on the repository. License This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_mcp_client-0.0.7.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_mcp_client-0.0.7-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file py_mcp_client-0.0.7.tar.gz.

File metadata

  • Download URL: py_mcp_client-0.0.7.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for py_mcp_client-0.0.7.tar.gz
Algorithm Hash digest
SHA256 d3e10d00875ece8cdadcb12d2be667443ce7f9c72b3731f3a982420a822419c7
MD5 c39440ccf5aefcaee2de49f37c751609
BLAKE2b-256 75efa41e91d0e5765a6bc132d439f0fa2acc99a73fddeb1cb1f7b560f975f596

See more details on using hashes here.

File details

Details for the file py_mcp_client-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: py_mcp_client-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for py_mcp_client-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 b8e873aa5403b89100eba502151491a02f7b166c1824b02514b2709c855d4867
MD5 32b070ea6ecd88a79730b3be4f51f105
BLAKE2b-256 6d9c04ef0cf973d5b0d03b204210069e5e03ccb0ec5e33b6a0b662f109b22583

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page