MCPServer
for building MCP-compatible environments that work with any MCP client.
MCPServer
Parameter | Type | Description | Default |
---|---|---|---|
name | str | Server name for MCP handshake | Required |
instructions | str | Server instructions/description | None |
**fastmcp_kwargs | Any | Additional FastMCP parameters | - |
- SIGTERM handling - Graceful shutdown in containers via custom runner
- Initialize decorator - Async setup during MCP initialize request (stdout is temporarily redirected to stderr during initialization to avoid corrupting MCP output)
- Shutdown decorator - Runs only on SIGTERM (container termination), not on hot‑reload/SIGINT
- Enhanced add_tool() - Automatically handles
BaseTool
instances and raw FastMCP Tool objects - Tool decorator passthrough -
@mcp.tool
returns the original function for easy composition - FastMCP inheritance - All FastMCP methods available (
mount
,resource
,tool
)
Decorators
@initialize
Run async setup during MCP initialize request:@shutdown
Run cleanup on SIGTERM (container termination only):Tool Registration
Three ways to register tools:Hub Pattern (mount)
Use BaseHub for organized tool namespaces:Resources
Expose metadata via MCP resources:Running the Server
GET /hud
– overviewGET /hud/tools
– list tools with schemasGET /hud/resources
– list resourcesGET /hud/prompts
– list prompts
Real Environment Examples
Minimal Environment
text_2048 Environment
Fromenvironments/text_2048/src/hud_controller/server.py
:
remote_browser Environment
Fromenvironments/remote_browser/src/hud_controller/server.py
:
Standard Structure
Directory Layout
Dockerfile
Hub Module Pattern
Example from text_2048:Key Concepts
Environment State
Three patterns for managing state:-
Global variables (simple environments):
-
Context class (complex environments):
-
Hub env attribute (for tool access):
Tool Lifecycle
- Setup tools - Hidden from agents, prepare environment state
- Interaction tools - Available to agents for control
- Evaluate tools - Hidden from agents, score performance
Progress Notifications
Send progress updates during long-running operations:Progress notifications follow the MCP progress specification. The
progressToken
comes from the client’s request metadata.Metadata Access
Agent metadata flows through initialization:Testing
See Also
- Build Environments - Getting started guide
- Tools - Tool implementation reference
- Environment Spec - Technical specification and architecture