服务文档
本项目基于FastMCP框架实现模型控制协议服务,提供加法计算的同步工具调用、个性化问候语生成的动态资源路由,并符合Model Control Protocol规范。
服务介绍
Demo 🚀 Service Documentation
License
🌟 Project Overview
This project implements a Model Control Protocol service based on the FastMCP framework, providing:
- Synchronous tool invocation: Implements addition calculation functionality
- Dynamic resource routing: Supports personalized greeting generation
- Standard protocol support: Complies with the Model Control Protocol specification
🧠 Technical Architecture
mermaid
graph TD
A[Client] -->|HTTP/WebSocket| B(FastMCP Service)
B --> C{Function Modules}
C --> D[Tool Invocation]
C --> E[Resource Routing]
D --> F[add function]
E --> G[greeting resource]
B --> H[Pydantic Data Validation]
B --> I[SSE Streaming Response]
🛠️ Environment Requirements
Component Version Requirement
Python 3.10+
FastMCP 0.1.0+
Pydantic 2.0.0+
Uvicorn 0.22.0+
📦 Installation Guide
bash
Create a virtual environment
python -m venv venv
source venv/bin/activate
Install dependencies
pip install fastmcp pydantic uvicorn sse-starlette
🚀 Quick Start
bash
Start the service (stdio mode)
python main.py
Or start using Uvicorn (recommended for production)
uvicorn main:app --reload
🧩 Core Features
- Tool Invocation
add
python
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two numbers"""
return a + b
Invocation Example:
json
{
"method": "add",
"params": {"a": 2, "b": 3},
"response": 5
}
- Dynamic Resource
greeting://{name}
python
@mcp.resource("greeting://{name}")
def get_greeting(name: str) -> str:
"""Get a personalized greeting"""
return f"Hello, {name}!"
Access Example:
bash
GET /greeting://Alice
Returns: "Hello, Alice!"
📡 API Specification
Tool Invocation Interface
Method: POST /tool_call
Request Body:
json
{
"method": "add",
"params": {"a": 2, "b": 3}
}
Response:
json
{
"result": 5
}
Resource Access Interface
Method: GET /greeting://{name}
Path Parameters:
- name: The name to be greeted
🛡️ Service Deployment
Production Environment Configuration
bash
Deploy using Uvicorn
uvicorn main:app --host 0.0.0.0 --port 8000 --workers 4
Docker Deployment Example
dockerfile
FROM python:3.10-slim
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
📐 Development Guidelines
- Type Annotations: All functions must use Python type annotations.
- Docstrings: Each tool/resource must include a docstring description.
- Exception Handling: Use PydanticCustomError for error handling.
- Logging: Use the standard logging module to output logs.
- Code Style: Follow PEP8 guidelines.
🤝 Contributing
- Fork the repository
- Create a feature branch
git checkout -b feature/xxx - Commit your changes
git commit -am 'Add xxx' - Push the branch
git push origin feature/xxx - Create a PR and attach a description of the changes
📄 License
MIT License (to be replaced with the actual license)
📌 Notes
For production environments, it is necessary to add:
- JWT authentication
- Request rate limiting strategies
- Prometheus monitoring metrics
- Distributed tracing support
- Fault tolerance mechanisms
The service uses stdio communication by default. It is recommended to switch to HTTP mode in production environments:
python
if name == "main":
mcp.run(transport="http", port=8000)