Efficient MCP server for standardized AI model interaction with FastAPI, JSON-RPC, SSE, and modular design
MCP Server is an advanced implementation of the Model Context Protocol (MCP), designed to standardize context interaction between AI models and development environments. Built on FastAPI, this server offers a robust infrastructure for integrating AI applications with diverse data sources and tools through a unified protocol. It leverages JSON-RPC 2.0 and Server-Sent Events (SSE) connections to ensure efficient, real-time communication and support for modular extensions.
MCP Server enhances the scalability and maintainability of AI applications by simplifying model deployment and ensuring consistency in input and output. Its modular design and asynchronous processing capabilities make it a versatile choice for developers looking to integrate and manage complex AI workflows efficiently.
MCP Server implements the JSON-RPC 2.0 protocol, which provides a request-response mechanism for structured data in JSON format. This ensures that both clients and servers can communicate effectively, facilitating the exchange of complex information necessary for AI applications.
Server-Sent Events (SSE) connections enable real-time notifications from the server to the client, enhancing user experience by providing instant updates as soon as relevant data becomes available. These connections are crucial for maintaining constant communication between different components in an AI application.
Utilizing FastAPI and asynchronous IO, MCP Server offers high performance and efficiency. This design ensures that the server can handle multiple clients simultaneously without significant latency, making it suitable for large-scale deployments.
The modular architecture of MCP Server allows for easy extension and customization. Developers can add new methods or modify existing functionalities to tailor the server to specific needs, ensuring flexibility in integration with various AI applications.
MCP Server follows a client-server model where the client initiates connections, requests data from the server, and receives responses in JSON-RPC format. The protocol flow diagram below illustrates this interaction process:
graph TD
A[AI Application] -->|MCP Client| B[MCP Protocol]
B --> C[MCP Server]
C --> D[Data Source/Tool]
style A fill:#e1f5fe
style C fill:#f3e5f5
style D fill:#e8f5e8
The server implements the model context protocol, providing a standardized way for AI applications to interact with different data sources and tools. This standardization ensures that any compatible client can connect seamlessly to MCP Server.
To get started with MCP Server, follow these steps:
Clone the Repository:
git clone https://github.com/freedanfan/mcp_server.git
cd mcp_server
Install Dependencies:
pip install -r requirements.txt
Run the Server (Default Configuration):
python mcp_server.py
By default, the server will run on 127.0.0.1:12000
. You can customize the host and port using environment variables:
export MCP_SERVER_HOST=0.0.0.0
export MCP_SERVER_PORT=8000
python mcp_server.py
Run the Client (Optional): Run the client in another terminal if needed, adjust the server URL as necessary:
export MCP_SERVER_URL="http://your-server-address:port"
python mcp_client.py
Imagine a scenario where an e-commerce platform needs to generate product descriptions based on given prompts. By integrating MCP Server, the platform can efficiently communicate with various text generation models without requiring each model to have a separate API endpoint.
Implementation Steps:
In a customer support system, integrating an MCP-server allows for seamless interaction between multiple chatbots and real-time database queries. This integration ensures that customer interactions are contextually accurate and personalized.
Implementation Steps:
MCP Server is compatible with several popular MCP clients such as Claude Desktop, Continue, Cursor, etc. The following table summarizes the compatibility matrix:
MCP Client | Resources | Tools | Prompts | Status |
---|---|---|---|---|
Claude Desktop | ✅ | ✅ | ✅ | Full Support |
Continue | ✅ | ✅ | ✅ | Full Support |
Cursor | ❌ | ✅ | ❌ | Tools Only |
To further enhance the server's functionality:
Environment Variables:
export MCP_SERVER_HOST=0.0.0.0
export MCP_SERVER_PORT=8000
Logging Setup:
# Increase log level
export PYTHONPATH=.
python -m logging -v DEBUG -m mcp_server
Customizing Methods: Extend the server to handle new methods by adding handlers and registering them as shown in the code snippet:
def _register_methods(self):
# Register existing methods
self.router.register_method("initialize", self.handle_initialize)
self.router.register_method("sample", self.handle_sample)
self(router.register_method("shutdown", self.handle_shutdown)
# Add new method
self.router.register_method("new_method", self.handle_new_method)
You can modify the handle_sample
method to include your AI model logic, as demonstrated below:
async def handle_sample(self, params: dict) -> dict:
"""Handle sampling request"""
logger.info(f"Received sampling request: {params}")
# Get prompt
prompt = params.get("prompt", "")
# Call AI model API (e.g., OpenAI)
response = await openai.ChatCompletion.acreate(
model="gpt-4",
messages=[{"role": "user", "content": prompt}]
)
content = response.choices[0].message.content
usage = response.usage
return {
"content": content,
"usage": {
"prompt_tokens": usage.prompt_tokens,
"completion_tokens": usage.completion_tokens,
"total_tokens": usage.total_tokens
}
}
Check the following:
Yes, it supports customization through method extensions. You can add new methods or modify existing ones by following the example provided in the code snippet.
This documentation adheres to the following criteria:
MCP Server stands out as a robust solution for standardizing context interaction between AI applications and diverse data sources, making it an essential tool for developers aiming to streamline their workflows and enhance application performance.
RuinedFooocus is a local AI image generator and chatbot image server for seamless creative control
Simplify MySQL queries with Java-based MysqlMcpServer for easy standard input-output communication
Learn to set up MCP Airflow Database server for efficient database interactions and querying airflow data
Build stunning one-page websites track engagement create QR codes monetize content easily with Acalytica
Explore CoRT MCP server for advanced self-arguing AI with multi-LLM inference and enhanced evaluation methods
Access NASA APIs for space data, images, asteroids, weather, and exoplanets via MCP integration