Skip to content

feat(backend): embed MCP server in WebApi for Claude Code#456

Open
fpindej wants to merge 3 commits intomasterfrom
feat/mcp-server
Open

feat(backend): embed MCP server in WebApi for Claude Code#456
fpindej wants to merge 3 commits intomasterfrom
feat/mcp-server

Conversation

@fpindej
Copy link
Owner

@fpindej fpindej commented Mar 14, 2026

Summary

  • Add a dev-only MCP server endpoint at /mcp with 6 tools (get-health, query-database, get-schema, list-users, list-jobs, trigger-job) that give Claude Code direct access to the running application through the existing DI container
  • Gate by IsDevelopment() so production builds never expose the endpoint
  • Add .mcp.json at project root for automatic Claude Code connection after init

Breaking Changes

None

Test Plan

  • Start Aspire, open Claude Code, verify 6 tools appear and respond
  • Verify MCP endpoint is not available when ASPNETCORE_ENVIRONMENT=Production
  • query-database rejects INSERT/UPDATE/DELETE/DROP statements
  • query-database limits results to 100 rows
  • Backend: dotnet build src/backend/MyProject.slnx && dotnet test src/backend/MyProject.slnx -c Release

fpindej added 2 commits March 14, 2026 12:52
Add a dev-only MCP (Model Context Protocol) endpoint at /mcp that gives
Claude Code direct access to the running application through 6 tools:
get-health, query-database, get-schema, list-users, list-jobs, and
trigger-job. All tools reuse existing DI services with zero duplication.

Gated by IsDevelopment() so production builds never expose the endpoint.
The .mcp.json at the project root configures Claude Code to connect
automatically after init scripts replace {INIT_API_PORT}.
@fpindej fpindej added documentation Improvements or additions to documentation backend Backend (.NET) feature New feature or enhancement labels Mar 14, 2026
@fpindej fpindej self-assigned this Mar 14, 2026
- Remove InternalsVisibleTo for WebApi from Infrastructure.csproj
- Rewrite DatabaseTools to use NpgsqlConnection from connection string
  instead of DbContext (no Infrastructure internals dependency)
- Enforce read-only at database level via SET TRANSACTION READ ONLY
  (replaces fragile keyword blocklist)
- Allow WITH (CTE) queries alongside SELECT
- Wrap user SQL in subquery for reliable LIMIT enforcement
- Query information_schema for get-schema instead of EF Core model
- Stop leaking exception messages (generic error for query failures,
  boolean hasError for health checks instead of ex.Message)
- Restrict MCP endpoint to localhost via RequireHost
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Backend (.NET) documentation Improvements or additions to documentation feature New feature or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant