This document provides comprehensive guidance for developing and extending the AWS IoT SiteWise MCP server. The server is built using Python and the FastMCP framework, providing a complete interface to AWS IoT SiteWise functionality.
awslabs/aws_iot_sitewise_mcp_server/
├── server.py # Main MCP server entry point
├── utils.py # Utility functions
├── tools/ # MCP tools organized by functionality
│ ├── sitewise_assets.py # Asset management tools
│ ├── sitewise_asset_models.py # Asset model management tools
│ ├── sitewise_data.py # Data ingestion and retrieval tools
│ ├── sitewise_gateways.py # Gateway and time series tools
│ └── sitewise_access.py # Access control and configuration tools
├── prompts/ # Intelligent prompts for common scenarios
│ ├── asset_hierarchy.py # Asset hierarchy visualization
│ ├── data_ingestion.py # Data ingestion guidance
│ └── dashboard_setup.py # Dashboard setup assistance
└── __init__.py
Tools are organized into logical modules based on AWS IoT SiteWise functionality:
- Assets (
sitewise_assets.py): Core asset lifecycle management - Asset Models (
sitewise_asset_models.py): Asset model definitions and management - Data (
sitewise_data.py): Data ingestion, retrieval, and analytics - Gateways (
sitewise_gateways.py): Edge gateway and time series management - Access (
sitewise_access.py): Security, access control, and configuration
- Python 3.10+: Required for development and testing
- uv package manager: For dependency management and virtual environments
- AWS Credentials: Configure with IoT SiteWise permissions
When developing new tools or features, you can test them with MCP clients:
After installing with uv tool install ., configure your MCP client:
{
"mcpServers": {
"aws-iot-sitewise-dev": {
"command": "uvx",
"args": ["awslabs.aws-iot-sitewise-mcp-server"],
"env": {
"AWS_REGION": "us-west-2",
"AWS_PROFILE": "your-dev-profile",
"FASTMCP_LOG_LEVEL": "DEBUG"
},
"transportType": "stdio"
}
}
}For development with hot reloading after code changes:
-
Reinstall after changes:
uv tool install . --force -
Or use development mode with direct execution:
{ "mcpServers": { "aws-iot-sitewise-dev": { "command": "uv", "args": [ "--directory", "/path/to/your/project", "run", "python", "-m", "awslabs.aws_iot_sitewise_mcp_server.server" ], "env": { "AWS_REGION": "us-west-2", "AWS_PROFILE": "your-dev-profile", "FASTMCP_LOG_LEVEL": "DEBUG" }, "transportType": "stdio" } } }
-
Clone Repository:
git clone https://github.com/awslabs/mcp.git cd src/aws-iot-sitewise-mcp-server -
Install as UV Tool:
# Install as a uv tool (makes it available globally via uvx) uv tool install . # Test the installation uvx awslabs.aws-iot-sitewise-mcp-server --help
-
For Development Work, Also Install Dev Dependencies:
# Install development dependencies uv sync --group dev -
Run Tests:
uv run --frozen pytest --cov --cov-branch --cov-report=term-missing
-
Format Code:
flake8
-
Clone Repository:
git clone https://github.com/awslabs/mcp.git cd src/aws-iot-sitewise-mcp-server -
Create Virtual Environment and Install Dependencies:
uv venv source .venv/bin/activate # On Windows: .venv\Scripts\activate uv pip install -e ".[dev]"
-
Run Tests:
pytest
-
Format Code:
flake8
All tools follow a consistent pattern for reliability and maintainability:
from awslabs.aws_iot_sitewise_mcp_server.client import create_sitewise_client
def tool_function(
required_param: str,
region: str = "us-east-1",
optional_param: Optional[str] = None
) -> Dict[str, Any]:
"""
Tool description with clear purpose and usage.
Args:
required_param: Description of required parameter
region: AWS region (default: us-east-1)
optional_param: Description of optional parameter
Returns:
Dictionary containing operation response
"""
try:
client = create_sitewise_client(region)
params = {'requiredParam': required_param}
if optional_param:
params['optionalParam'] = optional_param
response = client.api_operation(**params)
return {
'success': True,
'data': response['relevantData'],
# Include other relevant response fields
}
except ClientError as e:
return {
'success': False,
'error': str(e),
'error_code': e.response['Error']['Code']
}
# Create MCP tool
tool_name_tool = Tool.from_function(
fn=tool_function,
name="sitewise_tool_name",
description="Clear description of what the tool does and when to use it."
)- Consistent Error Handling: Always catch
ClientErrorand return structured error responses - Clear Documentation: Include comprehensive docstrings with parameter descriptions
- Flexible Parameters: Support optional parameters with sensible defaults
- Structured Responses: Return consistent response format with success/error indicators
- Regional Support: Always include region parameter with default value
- Choose the Right Module: Add the tool to the appropriate module based on functionality
- Implement the Function: Follow the standard pattern above
- Create the Tool: Use
Tool.from_functionto create the MCP tool - Register the Tool: Add to the appropriate tool list in
server.py - Write Tests: Add comprehensive tests in the
test/directory - Update Documentation: Add the tool to the README.md tools reference
# In sitewise_assets.py
def update_asset_property(
asset_id: str,
property_id: str,
property_alias: Optional[str] = None,
property_notification_state: str = "ENABLED",
region: str = "us-east-1"
) -> Dict[str, Any]:
"""
Update an asset property configuration.
Args:
asset_id: The ID of the asset
property_id: The ID of the property to update
property_alias: The alias for the property
property_notification_state: The notification state (ENABLED, DISABLED)
region: AWS region (default: us-east-1)
Returns:
Dictionary containing update response
"""
try:
client = create_sitewise_client(region)
params = {
'assetId': asset_id,
'propertyId': property_id,
'propertyNotificationState': property_notification_state
}
if property_alias:
params['propertyAlias'] = property_alias
client.update_asset_property(**params)
return {'success': True, 'message': 'Asset property updated successfully'}
except ClientError as e:
return {
'success': False,
'error': str(e),
'error_code': e.response['Error']['Code']
}
# Create the tool
update_asset_property_tool = Tool.from_function(
fn=update_asset_property,
name="sitewise_update_asset_property",
description="Update an asset property's configuration including alias and notification settings."
)Prompts provide intelligent guidance for complex IoT SiteWise scenarios:
def scenario_prompt(param1: str, param2: str) -> Prompt:
"""
Generate guidance for a specific IoT SiteWise scenario.
Args:
param1: Description of first parameter
param2: Description of second parameter
Returns:
Prompt for scenario guidance
"""
return Prompt(
messages=[
{
"role": "user",
"content": {
"type": "text",
"text": f"""
You are an AWS IoT SiteWise expert helping with {scenario description}.
Context:
- Parameter 1: {param1}
- Parameter 2: {param2}
Please provide step-by-step guidance including:
1. **Analysis Phase**:
- Use relevant sitewise tools to gather information
- Analyze current state and requirements
2. **Planning Phase**:
- Design approach and architecture
- Identify required resources and configurations
3. **Implementation Phase**:
- Provide specific tool calls and configurations
- Include error handling and validation steps
4. **Validation Phase**:
- Test and verify the implementation
- Provide troubleshooting guidance
Format your response with clear sections, specific tool calls, and actionable recommendations.
"""
}
}
]
)- Clear Context: Provide specific context and parameters
- Structured Guidance: Break down complex scenarios into manageable steps
- Tool Integration: Reference specific MCP tools to use
- Actionable Output: Provide concrete steps and configurations
- Error Handling: Include troubleshooting and validation steps
Tests are organized to match the tool structure:
test/
├── test_sitewise_assets.py # Asset management tool tests
├── test_sitewise_asset_models.py # Asset model tool tests
├── test_sitewise_data.py # Data operation tool tests
├── test_sitewise_gateways.py # Gateway tool tests
└── test_sitewise_access.py # Access control tool tests
- Mock AWS Clients: Use
@patchto mock boto3 clients - Test Success Cases: Verify correct responses and client calls
- Test Error Cases: Verify proper error handling
- Test Parameter Validation: Ensure parameters are passed correctly
@patch('awslabs.aws_iot_sitewise_mcp_server.tools.sitewise_assets.create_sitewise_client')
def test_create_asset_success(self, mock_boto_client):
"""Test successful asset creation."""
# Mock setup
mock_client = Mock()
mock_boto_client.return_value = mock_client
mock_client.create_asset.return_value = {
'assetId': 'test-asset-123',
'assetArn': 'arn:aws:iotsitewise:us-east-1:123456789012:asset/test-asset-123',
'assetStatus': {'state': 'CREATING'}
}
# Function call
result = create_asset(
asset_name="Test Asset",
asset_model_id="test-model-456"
)
# Assertions
assert result['success'] is True
assert result['asset_id'] == 'test-asset-123'
mock_client.create_asset.assert_called_once_with(
assetName="Test Asset",
assetModelId="test-model-456"
)The project uses several tools to maintain code quality:
- Black: Code formatting
- isort: Import sorting
- flake8: Linting and style checking
- mypy: Static type checking
Run all checks:
black awslabs test # Format code
isort awslabs test # Sort imports
flake8 awslabs test # Lint code
mypy awslabs # Type checking
pytest # Run testsAll functions should include comprehensive type hints:
from typing import Dict, List, Optional, Any
def example_function(
required_str: str,
optional_list: Optional[List[str]] = None,
region: str = "us-east-1"
) -> Dict[str, Any]:
"""Function with proper type hints."""
pass- Docstrings: Use Google-style docstrings for all functions
- Parameter Documentation: Document all parameters with types and descriptions
- Return Documentation: Clearly describe return values and structure
- Examples: Include usage examples for complex functions
The project uses uv for package management and building:
# Build distribution packages
uv build
# Install locally in development mode
uv pip install -e .
# Install from PyPI (if published)
uv pip install awslabs.aws-iot-sitewise-mcp-server- Import Errors: Ensure all dependencies are in
pyproject.toml - AWS Permissions: Verify IAM permissions for IoT SiteWise operations
- Region Issues: Check AWS region configuration and availability
- Tool Registration: Ensure new tools are added to
server.py
- Enable Logging: Add logging to tools for debugging
- Test Isolation: Use mocks to isolate AWS API calls during testing
- Error Messages: Provide clear error messages with context
- Validation: Add parameter validation for better error handling
- Batch Operations: Use batch APIs when available for better performance
- Pagination: Handle pagination properly for large result sets
- Caching: Consider caching for frequently accessed data
- Rate Limiting: Implement proper retry logic for rate-limited APIs
- Create Feature Branch: Branch from main for new features
- Implement Changes: Follow development patterns and standards
- Add Tests: Ensure comprehensive test coverage
- Update Documentation: Update README and relevant documentation
- Code Review: Submit PR for review and feedback
- Follows established patterns and conventions
- Includes comprehensive error handling
- Has appropriate test coverage
- Documentation is updated
- Type hints are complete
- Code is formatted and linted
- Advanced Analytics: Add support for IoT Analytics integration
- Alarm Management: Comprehensive alarm configuration and management
- Data Quality: Enhanced data validation and quality monitoring
- Performance Optimization: Caching and batch operation improvements
- Integration Patterns: Common integration patterns and templates
The architecture supports easy extension in several areas:
- New AWS Services: Add support for related AWS services
- Custom Prompts: Create domain-specific guidance prompts
- Validation Tools: Add data validation and quality checking tools
- Monitoring Tools: Enhanced monitoring and alerting capabilities
- Integration Helpers: Tools for common integration patterns