Skip to content

feat: add MCP server with sql_query, web_fetch, web_search tools - #62

Open
dimslaev wants to merge 6 commits into
masterfrom
claude/determined-wright-gyymk2
Open

feat: add MCP server with sql_query, web_fetch, web_search tools#62
dimslaev wants to merge 6 commits into
masterfrom
claude/determined-wright-gyymk2

Conversation

@dimslaev

Copy link
Copy Markdown
Owner

Adds a Model Context Protocol (MCP) server that exposes three tools for agents to query the database, fetch web pages, and search the web.

Key changes:

  • New MCP server (app/mcp/server.py) with bearer-token authentication gated by MCP_TOKEN setting
  • Three tools in app/mcp/tools.py:
    • sql_query: read-only database access via agentique_ro role, with statement planning to block writes and access to the user table, 1000-row result cap, and JSON serialization for timestamps/numerics
    • web_fetch: extracts readable text from a URL with proxy retry on 403/paywall/login blocks
    • web_search: Tavily web search with result ranking and optional domain filtering
  • New agentique_ro database role (passwordless on socket, password-protected over TCP) with SELECT-only grants and statement/lock timeouts
  • Settings for MCP token, read-only database user/password, and computed DSN with read-only transaction and 10s statement timeout
  • Mounted at /mcp/ in the main FastAPI app with CORS headers for SSE streaming
  • Vite proxy and Caddy reverse proxy configuration for /mcp endpoint
  • .mcp.json client config for agent integration
  • Comprehensive tests for query guards, row capping, type coercion, and tool forwarding
  • Workflow dispatch for SQL changed from read/write modes to write-only (reads now use MCP tool)

Implementation details:

  • Statement planning via EXPLAIN (FORMAT JSON) walks the entire plan tree to catch blocked tables in subqueries, CTEs, and joins
  • Prepared statements ensure only one statement executes, preventing semicolon-based injection
  • _json_safe fallback handles datetime, Decimal, UUID, and pgvector types for JSON serialization
  • Read-only transaction and role grants provide defense-in-depth against accidental writes

https://claude.ai/code/session_014AwCiyN5ksZ5QsHqXq8RQw

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants