MCP Client

by tony-nexartis

MCP Client 3 stars
  • other

About

What is MCP Client?

MCP Client is a Python application that interacts with Model Context Protocol (MCP) servers using Server-Sent Events (SSE). It is designed for developers who need real-time, persistent communication with MCP servers.

How to use MCP Client?

Install dependencies with pip install -r requirements.txt, then run python client_sse.py. Set the environment variables MCP_SERVER_URL, MCP_API_KEY, and ANTHROPIC_API_KEY before starting.

Key features of MCP Client

- Real‑time bidirectional communication via Server‑Sent Events
- Automatic session management with persistent connections
- Dynamic tool discovery and integration from the server
- Streaming responses for long‑running tool executions
- Robust error handling, timeouts, and reconnection attempts

Use cases of MCP Client

- Establishing persistent SSE connections to MCP servers for live updates
- Discovering and calling tools exposed by an MCP server in real time
- Executing tool calls and receiving streaming results through the SSE channel

FAQ from MCP Client

How does MCP Client use Server‑Sent Events (SSE)?

The client connects to an MCP server’s /sse endpoint, receives a session ID and a list of available tools via SSE events, and then sends tool calls via POST requests. Results and errors stream back over the same persistent connection.

What environment variables are required to run MCP Client?

You need MCP_SERVER_URL (server URL), MCP_API_KEY (for authentication), and ANTHROPIC_API_KEY (for Claude integration).

Does MCP Client support automatic reconnection if the connection drops?

Yes, the client implements automatic reconnection attempts and connection timeout detection to maintain a reliable session.

What programming language and libraries does MCP Client use?

MCP Client is written in Python and uses the aiohttp library for async HTTP and SSE handling.

Is MCP Client free and open source?

The README does not mention pricing or licensing. It is distributed as a Python script with a requirements file, but no license or cost information is provided.

Details

Author
tony-nexartis
GitHub stars
3
Category
other
Repository
tony-nexartis/anthropic_mcp_hackathon_sse_client

MCP Client

A Python client for interacting with Model Control Protocol (MCP) servers using Server-Sent Events (SSE).

Architecture

Server-Sent Events (SSE)

The client establishes a persistent connection with MCP servers using Server-Sent Events (SSE), enabling real-time, server-to-client communication. This architecture allows servers to push updates to the client without requiring constant polling.

Key SSE events:
- endpoint: Provides the session-specific messaging endpoint
- tools: Delivers available tools and capabilities
- error: Communicates server-side errors
- result: Returns tool execution results

Connection Flow

1. Client initiates SSE connection to /sse endpoint 2. Server responds with session ID via endpoint event 3. Server sends available tools via tools event 4. Client maintains persistent connection for real-time updates 5. Tool calls are made via POST requests to the SSE endpoint 6. Results stream back through the SSE connection

Usage

# Install dependencies
pip install -r requirements.txt

Run the client

python client_sse.py

Environment Variables

- MCP_SERVER_URL: URL of the MCP server
- MCP_API_KEY: API key for authentication
- ANTHROPIC_API_KEY: API key for Claude integration

Features

- Real-time bidirectional communication with MCP servers
- Automatic session management
- Dynamic tool discovery and integration
- Streaming responses for long-running operations
- Robust error handling and connection management

Server Implementation Guide

TODO: Adding SSE Support to an MCP Server

To modify an existing MCP server to support SSE, follow these steps:

1. Add SSE Dependencies

   import { EventEmitter } from 'events';
import { FastifyInstance } from 'fastify';

2. Create Session Management

   interface Session {
id: string;
emitter: EventEmitter;
tools: Tool[];
}

const sessions = new Map<string, Session>();

3. Implement SSE Endpoint

   server.get('/sse', async (request, reply) => {
const sessionId = generateSessionId();
const session = createSession(sessionId);

reply.raw.setHeader('Content-Type', 'text/event-stream');
reply.raw.setHeader('Cache-Control', 'no-cache');
reply.raw.setHeader('Connection', 'keep-alive');

// Send initial session info
reply.raw.write(event: endpoint\ndata: /messages/?session_id=${sessionId}\n\n);

// Send available tools
const tools = await getTools();
reply.raw.write(event: tools\ndata: ${JSON.stringify(tools)}\n\n);

// Handle client disconnect
request.raw.on('close', () => {
sessions.delete(sessionId);
});
});

4. Modify Tool Handlers

   server.post('/sse', async (request, reply) => {
const { session_id, tool, args } = request.body;
const session = sessions.get(session_id);

if (!session) {
throw new Error('Invalid session');
}

try {
const result = await executeToolCall(tool, args);
reply.raw.write(event: result\ndata: ${JSON.stringify(result)}\n\n);
} catch (error) {
reply.raw.write(event: error\ndata: ${JSON.stringify(error)}\n\n);
}
});

5. Update Server Configuration

   const server = new Server({
name: "mcp-sse-server",
version: "1.0.0"
}, {
capabilities: {
resources: {},
tools: {},
sse: true // Enable SSE capability
}
});

6. Error Handling

   server.setErrorHandler(async (error, request, reply) => {
const sessionId = request.body?.session_id;
const session = sessions.get(sessionId);

if (session) {
session.emitter.emit('error', error);
}
reply.code(500).send(error);
});

7. Session Cleanup

   function cleanupSessions() {
const now = Date.now();
for (const [id, session] of sessions.entries()) {
if (now - session.lastActive > SESSION_TIMEOUT) {
sessions.delete(id);
}
}
}

setInterval(cleanupSessions, CLEANUP_INTERVAL);

Key Considerations

1. Connection Management
- Implement heartbeat mechanism
- Handle reconnection gracefully
- Clean up inactive sessions

2. Security
- Validate session IDs
- Implement rate limiting
- Add authentication middleware

3. Performance
- Monitor memory usage for active sessions
- Implement connection pooling
- Add request queuing for heavy operations

4. Testing
- Add unit tests for SSE endpoints
- Test connection edge cases
- Verify tool execution through SSE

Error Handling

The client implements several error handling mechanisms:
- Connection timeout detection
- Automatic reconnection attempts
- Graceful error reporting
- Session state validation

Development

The client uses Python's aiohttp library for async HTTP and SSE handling.