Skip to content

refactor(server): move the MCP handlers' SQL into repositories - #50

Merged
fylorn merged 1 commit into
devfrom
refactor/server-mcp-repositories
Sep 24, 2026
Merged

fylorn merged 1 commit into
devfrom
refactor/server-mcp-repositories

Conversation

@fylorn

@fylorn fylorn commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

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 50 sqlx::query* calls move into four repositories under crates/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_servers reads/writes; the update transaction with its credential purge, the delete / bulk-delete transactions with the install-count decrement; last_error updates; 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; McpToolRow moves here (openapi import updated).

create_server and sync_registry interleave non-SQL work inside their transaction, so they keep the transaction and pass &mut PgConnection into the repository. Every other transaction now lives in the repository. Local FromRow row types moved alongside their queries. insert_shared_credential_from_wizard and the upsert_credential / upsert_shared_credential handler 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_token at create, background last_error set/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 id from mcp_user_credentials, which has no id column. Follow-up PR.

Checks run locally

  • cargo fmt --all --check
  • cargo clippy --workspace --all-targets -- -D warnings
  • cargo clippy --workspace --lib -- -D warnings
  • cargo nextest run --workspace --lib --bins --tests — 687 passed
  • full integration suite (--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

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
fylorn force-pushed the refactor/server-mcp-repositories branch from 5b37ec1 to 50a30d4 Compare September 24, 2026 09:10
@fylorn
fylorn merged commit 9b5b326 into dev Sep 24, 2026
6 checks passed
@fylorn
fylorn deleted the refactor/server-mcp-repositories branch September 24, 2026 09:21
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.

1 participant