What is A Stockfish-powered chess engine exposed as an MCP server using FastMCP.?
ChessPal Chess Engine is a Stockfish-powered chess engine exposed as an MCP server using FastMCP. It calculates best moves via MCP tools accessible over SSE or stdio transports using an MCP client library. Features include robust Stockfish engine integration, UCI protocol implementation, comprehensive test suite, error handling, and support for FEN positions.
Documentation
ChessPal Chess Engine - A Stockfish-powered chess engine exposed as an MCP server using FastMCP
A Stockfish-powered chess engine exposed as an MCP server using FastMCP. Calculates best moves via MCP tools accessible over SSE (default) or stdio transports using an MCP client library. Part of the ChessPal project.
Features
Robust Stockfish engine integration with proper process management
Exposes engine functionality via the Model Context Protocol (MCP) using FastMCP.
Supports both SSE and stdio MCP transports for client interaction.
UCI protocol implementation for chess move generation
Place it in: engines/stockfish/<version>/<os>/<binary_name>
See engines/README.md for the exact directory structure
Set CHESSPAL_ENGINE_OS environment variable to match your system:
export CHESSPAL_ENGINE_OS=macos # For macOS
export CHESSPAL_ENGINE_OS=linux # For Linux
export CHESSPAL_ENGINE_OS=windows # For Windows
Option 3: Build from Source (Advanced)
Advanced users can compile Stockfish from source using our separate dylangames-engine repository. This option provides maximum control over the build configuration but requires C++ development experience.
Usage# Starting the Server
The server uses FastMCP with support for both Server-Sent Events (SSE) and stdio transports. You can start it using:
SSE Mode (Default)
poetry run python -m chesspal_mcp_engine.main\n\n# Or
poetry run python -m chesspal_mcp_engine.main --transport sse
This command starts the MCP server in SSE mode, which listens for SSE connections on the configured host and port (default: 127.0.0.1:9000). This mode is ideal for programmatic clients and agents that need to interact with the chess engine over HTTP.
You can also use the entry point command:
poetry run chesspal-mcp-engine
Stdio Mode
poetry run python -m chesspal_mcp_engine.main --transport stdio\n\n# Or
poetry run chesspal-mcp-engine --transport stdio
This command starts the MCP server in stdio mode, which communicates through standard input/output. This mode is useful for direct integration with tools like Claude Desktop or for testing purposes.
API Endpoints
The module exposes the following endpoints through FastMCP:
get_best_move_tool: Get the best move for a given chess position
validate_move_tool: Validate if a move is legal in a given position
get_legal_moves_tool: Get all legal moves in a given position
get_game_status_tool: Get the current game status (in progress, checkmate, etc.)
Example request using the MCP SSE client:
from mcp.client.sse import sse_client
from mcp import ClientSession
async def get_best_move():
# Connect to the SSE endpoint
async with sse_client("http://127.0.0.1:9000/sse", timeout=10.0) as streams:
# Create an MCP session
async with ClientSession(*streams) as session:
# Initialize the session
await session.initialize()
# Call the tool - Note: Arguments MUST be wrapped in a "request" field
result = await session.call_tool('get_best_move_tool', {
"request": { # Required wrapper field
"fen": "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
"move_history": []
}
})
print(f"Best move: {result.best_move_uci}") # e.g., "e2e4"
Request Format
The get_best_move_tool expects requests in the following format:
{
"request": {
"fen": "string", // Required: FEN string representing the position
"move_history": [] // Optional: List of previous moves in UCI format
}
}
Note: The outer "request" wrapper field is required for proper request validation.
Timeouts
The engine is configured with the following timeouts:
Engine calculation time: 1000ms by default (configurable via CHESSPAL_ENGINE_TIMEOUT_MS)
Response wait timeout: 30s (allows time for engine initialization and calculation)
SSE client connection timeout: 15s (configurable in client code)
These timeouts ensure reliable operation while allowing sufficient time for move calculation, even on slower systems or when the engine needs more time to process complex positions.
Environment Variables
The module uses the following environment variables for configuration:
CHESSPAL_ENGINE_PATH=/path/to/your/engine/binary
# Fallback configuration (used if CHESSPAL_ENGINE_PATH is not set/invalid)
CHESSPAL_ENGINE_NAME=stockfish # Default: stockfish
CHESSPAL_ENGINE_VERSION=17.1 # Default: 17.1
CHESSPAL_ENGINE_OS=macos # Default: auto-detected based on platform
CHESSPAL_ENGINE_BINARY=stockfish # Default: stockfish (include .exe for Windows)
# Engine parameters
CHESSPAL_ENGINE_DEPTH=10 # Default: 10
CHESSPAL_ENGINE_TIMEOUT_MS=1000 # Default: 1000
# MCP Server Configuration
MCP_HOST=127.0.0.1 # Default: 127.0.0.1
MCP_PORT=9000 # Default: 9000
# Logging configuration
ENVIRONMENT=development # Default: development
LOG_LEVEL=INFO # Default: INFO for production, DEBUG for development
See .env.example for a complete example configuration.
poetry run black .
poetry run isort .
poetry run flake8
poetry run pre-commit run --all-files
Using the mcp inspector:
poetry run mcp dev src/chesspal_mcp_engine/main.py
# In the inspector UI\n\n# STDIO configuration
Command: poetry
Arguments: run python -m chesspal_mcp_engine.main --transport stdio
# SSE\n\n# In a separate terminal run the app in SSE mode
poetry run python -m chesspal_mcp_engine.main\n\n# In the mcp inspector UI
Transport Type > SSE