AWS Labs Core Model Context Protocol Server
The `awslabs-core-mcp-server` library provides a Python implementation of an AWS Labs Model Context Protocol (MCP) server. It allows for the creation of in-memory MCP servers, facilitating communication and context management for models over WebSockets. The current version is 1.0.27, and releases appear to be driven by the needs of the broader MCP project rather than a fixed schedule.
Common errors
-
OSError: [Errno 98] Address already in use
cause The network port specified for the MCP server is already being used by another process or application.fixStop the process using the port, or configure the MCP server to listen on a different, available port (e.g., 8081, 9000). For the quickstart, modify `port = 8080` to another value. -
ModuleNotFoundError: No module named 'awslabs_core_mcp_server'
cause The `awslabs-core-mcp-server` package has not been installed in the active Python environment.fixInstall the package using pip: `pip install awslabs-core-mcp-server`. -
websockets.exceptions.ConnectionClosedOK: no close frame received
cause This error typically occurs when a client disconnects unexpectedly or without sending a proper WebSocket close frame, or if there's a network interruption.fixVerify client-side WebSocket implementation for proper connection management and graceful closures. Check network stability and firewall configurations. For server-side, it often indicates a client-side issue rather than a server bug.
Warnings
- gotcha The server heavily relies on Python's `asyncio` library. Users unfamiliar with asynchronous programming in Python might face challenges with correctly integrating, starting, and gracefully shutting down the server within their applications.
- gotcha By default, the quickstart and CLI examples often use a common port (e.g., 8080). This can lead to port contention if another application is already using that port, preventing the MCP server from starting.
Install
-
pip install awslabs-core-mcp-server
Imports
- InMemoryModelContextProtocolServer
from awslabs_core_mcp_server.model_context_protocol import InMemoryModelContextProtocolServer
Quickstart
import asyncio
import websockets
from awslabs_core_mcp_server.model_context_protocol import InMemoryModelContextProtocolServer
async def run_server():
# Example initial data (can be empty or loaded from a file)
initial_data = {
"context_id_1": {"model_id": "model_A", "context": {"key": "value"}}
}
# Initialize the in-memory MCP server with optional initial data
server_instance = InMemoryModelContextProtocolServer(initial_data=initial_data)
# Start the WebSocket server
port = 8080
host = "localhost"
print(f"Starting MCP server on ws://{host}:{port}")
async with websockets.serve(server_instance.handle_request, host, port):
await asyncio.Future() # Run forever
if __name__ == "__main__":
try:
asyncio.run(run_server())
except KeyboardInterrupt:
print("\nServer stopped gracefully.")