fix(mcp): make remote /api/v1/mcp data plane work for external agents#2309
Merged
Conversation
The remote MCP HTTP surface (ADR-0036) was non-functional for external agents: every call 401'd with a valid key, standard MCP clients couldn't connect, and writes threw "ql.insert is not a function". All three are the same family — the MCP route resolves data services differently from REST. - resolveExecutionContext getQl: resolve objectql from the per-request kernel directly (kernel.getServiceAsync) instead of the scoped resolveService, which in the multi-env runtime hands back an instance blind to the env's sys_api_key rows so the api-key never resolved (→ 401). REST already resolves identity this way (rest-server), so REST + MCP no longer drift. - extractApiKey: accept `Authorization: Bearer osk_<key>` (prefix-gated). Remote MCP clients (Claude Desktop / Cursor / Claude Code) send the key as a Bearer per the MCP spec; a better-auth session Bearer never carries the osk_ prefix, so the session path is unaffected. - callData create/update/delete: route through protocol.createData/updateData/ deleteData (mirroring the read paths and REST) instead of calling ql.insert on context.dataDriver, which in the multi-env runtime is a raw db driver with no ORM methods. Validated on a local isolated stack: tools/list 401->200 (X-API-Key AND Bearer), create_record succeeds, sys_* and no-auth stay fail-closed, REST unaffected. Unit: mcp 44/44, core api-key 10/10, runtime mcp/exec-ctx/keys 41/41. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
📓 Docs Drift CheckThis PR changes 2 package(s): 30 hand-written doc(s) reference the affected code and may need an implementation-accuracy re-verification:
|
CodeQL flagged `^(ApiKey|Bearer)\s+(.+)$` as polynomial-ReDoS: `\s+` and `.+` both match whitespace, so an all-whitespace tail backtracks O(n²). Anchor the capture to a non-whitespace first char (`\s+(\S.*)`) — linear, same behaviour for every valid header. Clears the pre-existing ApiKey alert too. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
Summary
The remote MCP HTTP surface (ADR-0036,
/api/v1/mcp) was non-functional for external agents — found via local end-to-end testing. Three bugs, all the same family: the MCP route resolves data services differently (and more buggily) than REST, so a key that authenticates on REST was rejected on MCP, standard MCP clients couldn't connect at all, and writes crashed.401on/api/v1/mcp, same key works on RESTresolveExecutionContext'sgetQlused scopedresolveService('objectql', envId), which in the multi-env runtime returns an instance that can't see the env'ssys_api_keyrows → key never resolves. REST resolves identity viakernel.getServiceAsync('objectql')directly (the same gotchahandleActionsworks around).Authorization: Bearer <key>rejectedextractApiKeyonly acceptedX-API-Key/ApiKey. Remote MCP clients (Claude Desktop / Cursor / Claude Code) send the key as a Bearer per the MCP spec.create/update/delete_record→ql.insert is not a functionbuildMcpBridgepassescontext.dataDriver(a raw db driver in the multi-env runtime) asql;callDatawrites calledql.insert/update/deletedirectly. Reads worked only because query/get preferprotocol.findData/getData.Changes (4 files, +74/-15)
runtime/http-dispatcher.ts—resolveExecutionContextgetQl: preferthis.kernel.getServiceAsync('objectql'), fall back to the scoped path. Aligns MCP identity resolution with REST.core/security/api-key.ts—extractApiKey: also acceptAuthorization: Bearer osk_<key>(prefix-gated). A better-auth session Bearer never carries theosk_prefix, so the session path is unaffected.runtime/http-dispatcher.ts—callDatacreate/update/delete: route throughprotocol.createData/updateData/deleteData(mirroring the read paths and REST) before falling back toql.Validation
Live, on a local isolated stack (cloud + multi-tenant objectos runtime, real env, real
sys_api_key):tools/list401 → 200with bothX-API-KeyandAuthorization: Bearercreate_recordsucceeds (wasql.insert is not a function);query_records/get_recordworksys_*objects stay fail-closed; no-auth and bad-key stay401Unit:
@objectstack/mcp44/44 ·coreapi-key 10/10 ·runtime(mcp / exec-context / keys / kernel-resolver) 41/41.Follow-ups (not in this PR)
query_recordspasseswherethrough toprotocol.findData/objectql, but the tool description only documents equality — agents won't discover operator/range filters. Worth enriching the tool schema.🤖 Generated with Claude Code