There is already a FastAPI to MCP project https://github.com/tadata-org/fastapi_mcp, which can be directly applied Consider directly integrating MCP with Tango server
Connect to MCP (Model Context Protocol) and drive it with a large model
Test Result:
A REST API service based on FastAPI and PyTango for interacting with Tango devices.
- Python 3.8+
- Tango server environment
- Clone the repository:
git clone https://github.com/Contemporaries/Tango-rest-server-fastapi.git
cd Tango-rest-server-fastapi
- Create and activate a virtual environment:
python -m venv .venv
source .venv/bin/activate # Linux/Mac
# or
.venv\Scripts\activate # Windows
- Install dependencies:
pip install -r requirements.txt
http(s)://your_host:8000/docs
IHEP-Tango-Rest-API/
├── assets/ # Static resource files
├── config/ # Configuration files
│ ├── env_config.py # Environment variable configuration
│ ├── log_config.py # Log configuration
├── controller/ # API controllers
│ ├── controller_attribute.py # Attribute-related interfaces
│ ├── controller_command.py # Command-related interfaces
│ ├── controller_db.py # Database-related interfaces
│ ├── controller_info.py # Information query interfaces
│ ├── controller_polling.py # Polling-related interfaces
│ └── controller_property.py # Property configuration interfaces
| └── controller_env.py # Environment variable interfaces
├── exception/ # Exception handling
├── model/ # Data models
├── service/ # Business logic layer
│ ├── service_attribute.py # Attribute services
│ ├── service_command.py # Command services
│ ├── service_db.py # Database services
│ ├── service_info.py # Information query services
│ ├── service_polling.py # Polling services
│ └── service_property.py # Property configuration services
│ └── service_env.py # Environment variable services
├── test/ # Test files
├── tools/ # Utility functions
├── enums/ # Enum definitions
├── main.py # Application entry point
└── requirements.txt # Project dependencies
└── .env # Environment variables
- Create a new controller file in the
controller
directory - Implement business logic in the
service
directory - Register the route in
main.py
Use GlobalException
for unified error handling:
from exception.global_exception import GlobalException
try:
# Business logic
except Exception as e:
raise GlobalException(str(e))
Use ResponseModel
to ensure a unified response format:
from model.response_models import ResponseModel
return ResponseModel(
code: int
success: bool
message: str | None = None
data: dict | str | list | int | bool | None = None
)