A specialized client library for FastAPI services using Model Context Protocol (MCP) over Server-Sent Events (SSE)
Project description
FastAPI MCP Client
A specialized client library specifically designed to work with fastapi-mcp servers that implement the Model Context Protocol (MCP) over Server-Sent Events (SSE). This library provides a seamless way to interact with MCP-enabled FastAPI services.
Installation
# Install with pip
pip install fastapi-mcp-client
# Or with UV
uv add fastapi-mcp-client
Quick Start
import asyncio
from fastapi_mcp_client import MCPClient
async def main():
async with MCPClient("http://localhost:8000") as client:
# Call a non-streaming operation
result = await client.call_operation("echo", {"message": "Hello, MCP!"})
print(f"Echo result: {result}")
# Call a streaming operation with SSE
stream = await client.call_operation(
"generate_numbers",
{"count": 5},
stream=True
)
async for event in stream:
print(f"Event: {event}")
asyncio.run(main())
Features
- MCP Protocol Support: Full implementation of the Model Context Protocol
- SSE Streaming: First-class support for Server-Sent Events (SSE) streaming
- Async-First Design: Fully async-compatible for high-performance applications
- Seamless Session Management: Handles MCP session establishment and message passing
- Error Handling: Comprehensive error handling with fallback mechanisms
- Type Annotations: Full type hints for better IDE integration and validation
Running Examples
The repository includes examples to help you get started. Here's how to run them:
1. Clone the Repository
git clone https://github.com/RooseveltAdvisors/fastapi-mcp-client.git
cd fastapi-mcp-client
2. Set Up Environment
# Create and activate virtual environment
uv venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install all dependencies including examples
uv sync --all-extras
3. Start the Example Server
# In one terminal
cd examples/server
python simpler_server.py
This starts a FastAPI server with several MCP-enabled endpoints:
echo: Returns the message you sendgenerate_numbers: Streams a sequence of numberssearch_documents: Simulates a document search with streaming resultscalculate: Evaluates a simple math expression
4. Run the SSE Client Example
# In another terminal (with virtual env activated)
cd examples
python sse_example.py
Advanced Usage
Custom Client Configuration
from fastapi_mcp_client import MCPClient, MCPClientConfig
config = MCPClientConfig(
base_url="http://localhost:8000",
timeout=60.0,
log_level="DEBUG"
)
async with MCPClient("http://localhost:8000", config=config) as client:
# Call a streaming tool with custom configuration
stream = await client.call_operation(
"generate_numbers",
{"count": 10},
stream=True
)
async for event in stream:
print(f"Received event: {event}")
Understanding the MCP/SSE Flow
sequenceDiagram
participant C as Client
participant S as Server
Note over C,S: Establish SSE Connection
C->>+S: GET /mcp (Accept: text/event-stream)
S-->>-C: 200 OK (Connection Open)
S-->>C: SSE: data: /mcp/messages/?session_id=XXX
Note over C: Parse session_id=XXX
Note over C,S: MCP Initialization
C->>+S: POST /mcp/messages/?session_id=XXX <br> Payload: {method: "initialize", ...}
S-->>-C: 202 Accepted
Note over C,S: MCP Tool Call
C->>+S: POST /mcp/messages/?session_id=XXX <br> Payload: {method: "tools/call", ...}
S-->>-C: 202 Accepted
Note over C,S: Stream Results
S-->>C: SSE: data: {result_part_1}
S-->>C: SSE: data: {result_part_2}
S-->>C: SSE: data: {final_result}
Note over S: (Closes SSE Connection or sends close event)
Examples
Check out the examples directory for more:
- Server Example: A sample FastAPI MCP server
- SSE Example: Advanced streaming with custom configuration
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Development Setup
-
Clone the repository
git clone https://github.com/RooseveltAdvisors/fastapi-mcp-client.git cd fastapi-mcp-client
-
Install uv
curl -LsSf https://astral.sh/uv/install.sh | sh
-
Create and activate a virtual environment
uv venv source .venv/bin/activate # On Windows: .venv\Scripts\activate
-
Install development dependencies
uv sync --all-extras
-
Run tests
pytest
Pull Request Process
- Create your feature branch (
git checkout -b feature/amazing-feature) - Make your changes and ensure tests pass
- Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Built to work with FastAPI MCP - a FastAPI extension for adding MCP support to your APIs
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_mcp_client-0.4.0.tar.gz.
File metadata
- Download URL: fastapi_mcp_client-0.4.0.tar.gz
- Upload date:
- Size: 81.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6af245d7cc7227e2588788e65910de6b53bb49447131be5a6763b046c5613c3a
|
|
| MD5 |
6e2a3f3da16a4cb645dfb9262f011411
|
|
| BLAKE2b-256 |
c7eceeec860ab7b6de590dbd2bf81d0896d970f47ac10420533d15135818fa6e
|
File details
Details for the file fastapi_mcp_client-0.4.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_mcp_client-0.4.0-py3-none-any.whl
- Upload date:
- Size: 11.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
78dde7e38d0be5ad8d48c4b0bb28136288c21ba834382dd8519068cb1d22f5bb
|
|
| MD5 |
f44269bb016c02fdbdd5179b3516548d
|
|
| BLAKE2b-256 |
613e8fa81fc5049555c0d837ccabd679efca90acd0b3763811433b3d4821722c
|