refactor(server): move the MCP handlers' SQL into repositories - #50
Merged
Merged
Conversation
The MCP server, connection, shared-credential, store and tool-catalog handlers no longer carry SQL. Their 50 statements move, text and binds unchanged, into four repositories under `services/`: - mcp_server_repository: mcp_servers reads and writes, the update and delete transactions (with their credential purge and install-count decrement), last_error updates, the unique-violation to 409 mapping - mcp_credential_repository: per-user and shared credentials, including the upsert, revoke and set-default transactions - mcp_store_repository: templates, installs, the registry-sync upsert and prune, the install advisory lock - mcp_tool_repository: the tool catalog count and page (McpToolRow moves here) Server create and registry sync keep their transaction in the handler and pass it to the repository, since they interleave non-SQL work. admin_mcp_catalog.rs covers the endpoints the suite didn't reach; it passes on the code before and after this change. Co-Authored-By: Claude Opus 5.5 <[email protected]>
fylorn
force-pushed
the
refactor/server-mcp-repositories
branch
from
September 24, 2026 09:10
5b37ec1 to
50a30d4
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What moved
The MCP handlers (
mcp_servers.rs,mcp_oauth.rs,mcp_oauth/shared.rs,mcp_oauth/wizard.rs,mcp_store.rs,mcp_tools.rs) no longer carry SQL. All 50sqlx::query*calls move into four repositories undercrates/server/src/services/, with the statement text byte-for-byte unchanged (checked mechanically against the old handlers), the same binds and the same fetch kind:mcp_server_repository—mcp_serversreads/writes; the update transaction with its credential purge, the delete / bulk-delete transactions with the install-count decrement;last_errorupdates; the unique-violation → 409 mapping (moved from the handler).mcp_credential_repository— per-user and shared credentials: the upsert (advisory lock + first-is-default), revoke-and-promote and set-default transactions, shared status/token reads, inserts used by server create.mcp_store_repository— templates, installs, the registry-sync upsert and prune, the install advisory lock (the lock-key constant moves with it).mcp_tool_repository— the tool catalog count and page;McpToolRowmoves here (openapi import updated).create_serverandsync_registryinterleave non-SQL work inside their transaction, so they keep the transaction and pass&mut PgConnectioninto the repository. Every other transaction now lives in the repository. LocalFromRowrow types moved alongside their queries.insert_shared_credential_from_wizardand theupsert_credential/upsert_shared_credentialhandler helpers are gone; callers use the repository directly. Permission checks, validation, encryption, registry/pool sync, cache invalidation and audit stay in the handlers, in the same order.No behaviour change
New
crates/test-support/tests/admin_mcp_catalog.rs(13 tests) covers what the MCP suite didn't reach: server get/patch/delete incl. 404s, name/prefix 409s on create and update, unknown template 404, active-only tool counts and list order,shared_static_tokenat create, backgroundlast_errorset/clear (create and shared-token paths), shared-credential status/paste/revoke, shared authorize preconditions, connection listing filters, set-default / revoke / 404s, template by slug, the registry sync (upsert, update, prune, installed kept, invalid template skipped, auth-shape derivation), categories, and the tool catalog's search / server filter / paging /include_user_tools.The new file passes on the code before this change (handlers stashed) and after it: 13/13 both times.
Found while writing them, not fixed here (verbatim SQL): revoking a user's default connection returns 500 — the promote statement selects
idfrommcp_user_credentials, which has noidcolumn. Follow-up PR.Checks run locally
cargo fmt --all --checkcargo clippy --workspace --all-targets -- -D warningscargo clippy --workspace --lib -- -D warningscargo nextest run --workspace --lib --bins --tests— 687 passed--run-ignored only --profile ci) on own containers: all MCP tests pass; the 37 ClickHouse-dependent failures in the first run were my ClickHouse container being OOM-killed mid-run, and all 37 pass on rerun after restarting it.🤖 Generated with Claude Code