This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
StarRocks Official MCP Server - A bridge between AI assistants and StarRocks databases, built using FastMCP framework. Enables direct SQL execution, database exploration, data visualization, and schema introspection through the Model Context Protocol (MCP).
Local Development:
# Run the server directly for testing
uv run mcp-server-starrocks
# Run with test mode to verify table overview functionality
uv run mcp-server-starrocks --test
# Run in Streamable HTTP mode (recommended for integration)
export MCP_TRANSPORT_MODE=streamable-http
uv run mcp-server-starrocksPackage Management:
# Install dependencies (handled by uv automatically)
uv sync
# Build package
uv buildsrc/mcp_server_starrocks/server.py: Main server implementation containing all MCP tools, resources, and database connection logicsrc/mcp_server_starrocks/__init__.py: Entry point that starts the async server
The server supports two connection modes:
- Standard MySQL Protocol: Default connection using
mysql.connector - Arrow Flight SQL: High-performance connection using ADBC drivers (enabled when
STARROCKS_FE_ARROW_FLIGHT_SQL_PORTis set)
Connection management uses a global singleton pattern with automatic reconnection handling.
-
Query Execution Tools:
read_query: Execute SELECT and other result-returning querieswrite_query: Execute DDL/DML commandsanalyze_query: Query performance analysis via EXPLAIN ANALYZE
-
Overview Tools with Caching:
table_overview: Get table schema, row count, and sample data (cached)db_overview: Get overview of all tables in a database (uses table cache)
-
Visualization Tool:
query_and_plotly_chart: Execute query and generate Plotly charts from results
starrocks:///databases: List all databasesstarrocks:///{db}/tables: List tables in a databasestarrocks:///{db}/{table}/schema: Get table CREATE statementproc:///{path}: Access StarRocks internal system information (similar to Linux /proc)
In-memory cache for table overviews using (database_name, table_name) as cache keys. Cache includes both successful results and error messages. Controlled by STARROCKS_OVERVIEW_LIMIT environment variable (default: 20000 characters).
Environment variables for database connection:
STARROCKS_HOST: Database host (default: localhost)STARROCKS_PORT: MySQL port (default: 9030)STARROCKS_USER: Username (default: root)STARROCKS_PASSWORD: Password (default: empty)STARROCKS_DB: Default database for sessionSTARROCKS_MYSQL_AUTH_PLUGIN: Auth plugin (e.g., mysql_clear_password)STARROCKS_FE_ARROW_FLIGHT_SQL_PORT: Enables Arrow Flight SQL modeMCP_TRANSPORT_MODE: Communication mode (stdio/streamable-http/sse)
- Database errors trigger connection reset via
reset_connection() - All tools return string error messages rather than raising exceptions
- Cursors are always closed in finally blocks
- SQL injection prevention through parameterized queries and backtick escaping
- Plotly expressions are validated using AST parsing to prevent code injection
- Limited
eval()usage with restricted scope for chart generation
- Tools are defined as async functions even though database operations are synchronous
- Main server runs in async context using
FastMCP.run_async()
This is a simple Python package built with hatchling:
- Single module in
src/mcp_server_starrocks/ - Entry point defined in pyproject.toml as
mcp-server-starrockscommand - Dependencies managed through pyproject.toml, no requirements.txt files