A Model Context Protocol (MCP) server that provides tools for interacting with the BodySpec API, enabling access to DEXA scan data and health information.
- Complete API Coverage: All BodySpec API endpoints are available as MCP tools
- Authentication: Bearer token authentication via
.envfile configuration - Error Handling: Comprehensive error handling and logging
- Type Safety: Full Pydantic model validation
- Async Support: Built with async/await for optimal performance
- MCP Protocol: Proper stdio transport implementation for MCP communication
bodyspec_api_info- Get API information and versionbodyspec_health_check- Check API health status
bodyspec_get_user_info- Get current user profilebodyspec_update_user_info- Update user profile information
bodyspec_list_appointments- List user appointments with filteringbodyspec_get_appointment- Get detailed appointment information
bodyspec_list_results- List user test resultsbodyspec_get_result_detail- Get detailed result information
bodyspec_dexa_scan_info- Get DEXA scan metadatabodyspec_dexa_composition- Get body composition data by regionbodyspec_dexa_bone_density- Get bone mineral density measurementsbodyspec_dexa_percentiles- Get age/gender-matched percentile rankingsbodyspec_dexa_visceral_fat- Get visceral adipose tissue analysis
-
Clone or download this repository
-
Install dependencies:
pip install -r requirements.txt
-
Set up authentication:
Step 1: Get your Bearer token
- Visit the BodySpec API documentation at https://app.bodyspec.com/docs
- Log in to your BodySpec account (if not already logged in)
- Navigate to the Authentication or API Keys section in the documentation
- Look for instructions on generating or retrieving your OAuth2 access token (Bearer token)
- Copy the token - it should be a long string of characters (JWT format)
Note: If you need help finding the token, check the MCP setup guide at https://app.bodyspec.com/#mcp-setup
Step 2: Configure the token
Create a
.envfile in the project root and add your token:# Copy the example file cp env_example.txt .env # Edit .env and add your token BODYSPEC_ACCESS_TOKEN=your_token_here
You can also configure other settings in the
.envfile (see Configuration section below).Step 3: Configure mcp.json for Cursor
Add the minimal configuration to your
mcp.jsonfile (required by Cursor):{ "mcpServers": { "BodySpec": { "command": "python", "args": ["/path/to/bodyspec-mcp/server.py"], "env": { "PYTHONUNBUFFERED": "1" } } } }Note: The token and other configuration values should only be set in the
.envfile, not inmcp.json. Themcp.jsonfile is only needed for Cursor to know how to run the server.
The server uses Bearer token authentication. You need to obtain a JWT access token from BodySpec's API documentation and configure it as shown above.
python server.pyThe server will start and be ready to accept MCP tool calls.
{
"name": "bodyspec_api_info",
"arguments": {}
}{
"name": "bodyspec_list_appointments",
"arguments": {
"limit": 10,
"sort_order": "newest_first"
}
}{
"name": "bodyspec_dexa_composition",
"arguments": {
"result_id": "your_result_id_here"
}
}{
"name": "bodyspec_dexa_percentiles",
"arguments": {
"result_id": "your_result_id_here",
"min_age": 30,
"max_age": 40
}
}The server is configured using environment variables in the .env file. Configuration sources are loaded in the following order of precedence (highest to lowest):
.envfile (highest precedence) - All configuration values should be set here- System environment variables - Variables set in your shell/system
mcp.jsonenv section - Only used for Cursor-specific settings likePYTHONUNBUFFERED
Note: The .env file is where you should configure your token and all other settings. The mcp.json file is only needed for Cursor to know how to run the server and should contain minimal configuration.
| Variable | Description | Default |
|---|---|---|
BODYSPEC_ACCESS_TOKEN |
Bearer token for API authentication | Required |
BODYSPEC_BASE_URL |
BodySpec API base URL | https://app.bodyspec.com |
BODYSPEC_API_VERSION |
API version | v1 |
BODYSPEC_TIMEOUT |
Request timeout in seconds | 30 |
BODYSPEC_MAX_RETRIES |
Maximum retry attempts | 3 |
BODYSPEC_LOG_LEVEL |
Logging level | INFO |
The server uses Pydantic models that match the BodySpec API specification:
- UserResponse: User profile information
- Appointment: Appointment details with location and service info
- ResultSummary/ResultDetailResponse: Test result information
- DexaCompositionResponse: Body composition data by region
- DexaBoneDensityResponse: Bone density measurements
- DexaPercentilesResponse: Age/gender-matched percentile rankings
- DexaVisceralFatResponse: Visceral adipose tissue analysis
The server provides comprehensive error handling:
- Authentication Errors: Invalid or expired tokens
- API Errors: HTTP errors from the BodySpec API
- Validation Errors: Invalid input parameters
- Network Errors: Timeout and connection issues
All errors are logged and returned as user-friendly messages.
bodyspec-mcp-server/
├── server.py # Main MCP server
├── config.py # Configuration management
├── auth.py # Authentication handling
├── api_client.py # BodySpec API client
├── models.py # Pydantic data models
├── tools/ # MCP tools directory
│ ├── __init__.py
│ ├── status_tools.py # API status tools
│ ├── user_tools.py # User management tools
│ ├── appointment_tools.py # Appointment tools
│ ├── result_tools.py # Results tools
│ └── dexa_tools.py # DEXA-specific tools
├── requirements.txt # Python dependencies
├── README.md # This file
└── example_usage.py # Usage examples
- Create a new tool module in the
tools/directory - Implement the
register_*_tools()function - Import and register the tools in
server.py
Test individual components:
# Test authentication
from auth import create_auth
auth = create_auth()
print(f"Authenticated: {auth.is_authenticated()}")
# Test API client
from api_client import create_client
import asyncio
client = create_client()
info = await client.get_api_info()
print(f"API Version: {info.version}")For issues with the BodySpec API, contact: [email protected]
For MCP server issues, check the logs (written to stderr) and ensure you have configured the BODYSPEC_ACCESS_TOKEN in your .env file.
This project is provided as-is for integrating with the BodySpec API. Please refer to BodySpec's terms of service for API usage guidelines.