1.7 KiB
1.7 KiB
Python Endpoints Demo
This example demonstrates how to create and use Python-based endpoints in MXCP.
Features Demonstrated
1. Basic Python Functions
analyze_numbers- Statistical analysis with various operationscreate_sample_data- Database operations from Python
2. Async Functions
process_time_series- Demonstrates async Python endpoint
3. Database Access
- Using
mxcp.runtime.dbto execute SQL queries - Parameter binding for safe SQL execution
Running the Examples
In a terminal, test the endpoints:
# Create sample data
mxcp run tool create_sample_data --param table_name=test_data --param row_count=100
# Analyze numbers
mxcp run tool analyze_numbers --param numbers="[1, 2, 3, 4, 5]" --param operation=mean
# Process time series (async function)
mxcp run tool process_time_series --param table_name=test_data --param window_days=7
Or, if you prefer, you can also start the MXCP server and use any MCP client to call the tools:
mxcp serve
Project Structure
python-demo/
├── mxcp-site.yml # Project configuration
├── python/ # Python modules
│ └── data_analysis.py # Python endpoint implementations
├── tools/ # Tool definitions
│ ├── analyze_numbers.yml
│ ├── create_sample_data.yml
│ └── process_time_series.yml
└── README.md
Key Concepts
- Language Declaration: Set
language: pythonin the tool definition - Function Names: The function name must match the tool name
- Return Types: Functions must return data matching the declared return type
- Database Access: Use
db.execute()for SQL queries - Async Support: Both sync and async functions are supported