-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Developer Knowledge OneMCP proxy #9921
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chkuang-g
wants to merge
10
commits into
main
Choose a base branch
from
chkuang/dkp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+389
−12
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b889fae
Developer Knowledge MCP proxy
chkuang-g 3b7973b
minor fix
chkuang-g 50fcb24
Improve error handling
chkuang-g 04ddac5
Address some feedback and lint
chkuang-g 3f51b94
Revert meaningless changes
chkuang-g b1b2a6b
spaces and Changelog
chkuang-g 7e67f85
cleanup
chkuang-g 705329c
Prettier
chkuang-g 25757e5
Ensure API is enabled for MCP tools/call
chkuang-g 390aa55
Add tests and fix existing tests
chkuang-g File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| - Added new DevKnowledge MCP tools |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { developerKnowledgeOrigin } from "../../api"; | ||
| import { ServerFeature } from "../types"; | ||
| import { OneMcpServer } from "./onemcp_server"; | ||
|
|
||
| export const ONEMCP_SERVERS: Partial<Record<ServerFeature, OneMcpServer>> = { | ||
| developerknowledge: new OneMcpServer("developerknowledge", developerKnowledgeOrigin()), | ||
| }; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| import { expect } from "chai"; | ||
| import * as sinon from "sinon"; | ||
| import { OneMcpServer } from "./onemcp_server"; | ||
| import { Client } from "../../apiv2"; | ||
| import * as ensureModule from "../../ensureApiEnabled"; | ||
| import { FirebaseError } from "../../error"; | ||
|
|
||
| describe("OneMcpServer", () => { | ||
| let sandbox: sinon.SinonSandbox; | ||
| let clientRequestStub: sinon.SinonStub; | ||
| let ensureStub: sinon.SinonStub; | ||
|
|
||
| const feature = "test_feature" as any; | ||
| const serverUrl = "https://example.com"; | ||
| let server: OneMcpServer; | ||
|
|
||
| beforeEach(() => { | ||
| sandbox = sinon.createSandbox(); | ||
| clientRequestStub = sandbox.stub(Client.prototype, "request"); | ||
| ensureStub = sandbox.stub(ensureModule, "ensure").resolves(); | ||
| server = new OneMcpServer(feature, serverUrl); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| sandbox.restore(); | ||
| }); | ||
|
|
||
| describe("fetchRemoteTools", () => { | ||
| it("should fetch and parse remote tools successfully", async () => { | ||
| const mockMcpTool = { | ||
| name: "test_tool", | ||
| description: "A test tool", | ||
| inputSchema: { type: "object", properties: {} }, | ||
| }; | ||
| clientRequestStub.resolves({ | ||
| body: { | ||
| result: { | ||
| tools: [mockMcpTool], | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const tools = await server.fetchRemoteTools(); | ||
|
|
||
| expect(tools).to.have.length(1); | ||
| expect(tools[0].mcp.name).to.equal("test_feature_test_tool"); | ||
| expect(tools[0].mcp.description).to.equal(mockMcpTool.description); | ||
| expect(tools[0].mcp._meta).to.deep.equal({ | ||
| requiresAuth: true, | ||
| requiresProject: true, | ||
| }); | ||
| expect(clientRequestStub).to.have.been.calledOnce; | ||
| }); | ||
|
|
||
| it("should throw FirebaseError if fetch fails", async () => { | ||
| clientRequestStub.rejects(new Error("Network Error")); | ||
|
|
||
| await expect(server.fetchRemoteTools()).to.be.rejectedWith( | ||
| FirebaseError, | ||
| /Failed to fetch remote tools/, | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe("proxyRemoteToolCall", () => { | ||
| const mockContext: any = { | ||
| projectId: "test-project", | ||
| }; | ||
|
|
||
| it("should call ensure and proxy tool call successfully", async () => { | ||
| const mockMcpTool = { name: "test_tool", inputSchema: { type: "object", properties: {} } }; | ||
| clientRequestStub.onFirstCall().resolves({ | ||
| body: { result: { tools: [mockMcpTool] } }, | ||
| }); | ||
|
|
||
| const tools = await server.fetchRemoteTools(); | ||
| const tool = tools[0]; | ||
|
|
||
| const mockCallResult = { content: [{ type: "text", text: "success" }] }; | ||
| clientRequestStub.onSecondCall().resolves({ | ||
| body: { result: mockCallResult }, | ||
| }); | ||
|
|
||
| const result = await tool.fn({ arg: "val" }, mockContext); | ||
|
|
||
| expect(result).to.deep.equal(mockCallResult); | ||
| expect(ensureStub).to.have.been.calledOnceWith( | ||
| mockContext.projectId, | ||
| serverUrl, | ||
| feature, | ||
| true, | ||
| ); | ||
| expect(clientRequestStub.secondCall.args[0]).to.deep.include({ | ||
| method: "POST", | ||
| body: { | ||
| method: "tools/call", | ||
| params: { | ||
| name: "test_tool", | ||
| arguments: { arg: "val" }, | ||
| }, | ||
| jsonrpc: "2.0", | ||
| id: 1, | ||
| }, | ||
| }); | ||
| expect(clientRequestStub.secondCall.args[0].headers).to.deep.include({ | ||
| "x-goog-user-project": "test-project", | ||
| }); | ||
| }); | ||
|
|
||
| it("should handle remote tool error results", async () => { | ||
| const mockMcpTool = { name: "test_tool", inputSchema: { type: "object", properties: {} } }; | ||
| clientRequestStub.onFirstCall().resolves({ | ||
| body: { result: { tools: [mockMcpTool] } }, | ||
| }); | ||
|
|
||
| const tools = await server.fetchRemoteTools(); | ||
| const tool = tools[0]; | ||
|
|
||
| const mockErrorResult = { isError: true, content: [{ type: "text", text: "remote error" }] }; | ||
| const firebaseError = new FirebaseError("Remote tool error", { | ||
| status: 400, | ||
| context: { | ||
| body: { | ||
| result: mockErrorResult, | ||
| }, | ||
| }, | ||
| }); | ||
| clientRequestStub.onSecondCall().rejects(firebaseError); | ||
|
|
||
| const result = await tool.fn({ arg: "val" }, mockContext); | ||
|
|
||
| expect(result).to.deep.equal(mockErrorResult); | ||
| }); | ||
|
|
||
| it("should throw original error if not a handled FirebaseError", async () => { | ||
| const mockMcpTool = { name: "test_tool", inputSchema: { type: "object", properties: {} } }; | ||
| clientRequestStub.onFirstCall().resolves({ | ||
| body: { result: { tools: [mockMcpTool] } }, | ||
| }); | ||
|
|
||
| const tools = await server.fetchRemoteTools(); | ||
| const tool = tools[0]; | ||
|
|
||
| const genericError = new Error("Generic Error"); | ||
| clientRequestStub.onSecondCall().rejects(genericError); | ||
|
|
||
| await expect(tool.fn({}, mockContext)).to.be.rejectedWith("Generic Error"); | ||
| }); | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { | ||
| CallToolResult, | ||
| CallToolResultSchema, | ||
| ListToolsResultSchema, | ||
| } from "@modelcontextprotocol/sdk/types.js"; | ||
| import { Client } from "../../apiv2"; | ||
| import { ServerTool } from "../tool"; | ||
| import { McpContext, ServerFeature } from "../types"; | ||
| import { FirebaseError } from "../../error"; | ||
| import { ensure } from "../../ensureApiEnabled"; | ||
|
|
||
| /** | ||
| * OneMcpServer encapsulates the logic for interacting with a remote MCP server. | ||
| */ | ||
| export class OneMcpServer { | ||
| private listClient: Client; | ||
| private callClient: Client; | ||
| constructor( | ||
| private readonly feature: ServerFeature, | ||
| private readonly serverUrl: string, | ||
| ) { | ||
| this.listClient = new Client({ | ||
| urlPrefix: this.serverUrl, | ||
| auth: false, | ||
| }); | ||
| this.callClient = new Client({ | ||
| urlPrefix: this.serverUrl, | ||
| auth: true, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Fetches tools from the remote MCP server. | ||
| */ | ||
| async fetchRemoteTools(): Promise<ServerTool[]> { | ||
| try { | ||
| const res = await this.listClient.post<any, any>("/mcp", { | ||
| method: "tools/list", | ||
| jsonrpc: "2.0", | ||
| id: 1, | ||
| }); | ||
|
|
||
| const parsed = ListToolsResultSchema.parse(res.body.result); | ||
| return parsed.tools.map((mcpTool) => ({ | ||
| mcp: { | ||
| ...mcpTool, | ||
| name: `${this.feature}_${mcpTool.name}`, | ||
| _meta: { | ||
| requiresAuth: true, | ||
| requiresProject: true, | ||
| }, | ||
| }, | ||
| fn: (args: any, ctx: McpContext) => this.proxyRemoteToolCall(mcpTool.name, args, ctx), | ||
| isAvailable: () => Promise.resolve(true), | ||
| })); | ||
| } catch (error) { | ||
| throw new FirebaseError( | ||
| "Failed to fetch remote tools for " + this.serverUrl + ": " + JSON.stringify(error), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Proxies a tool call to the remote MCP server. | ||
| */ | ||
| private async proxyRemoteToolCall( | ||
| toolName: string, | ||
| args: any, | ||
| ctx: McpContext, | ||
| ): Promise<CallToolResult> { | ||
| await ensure(ctx.projectId, this.serverUrl, this.feature, /* silent=*/ true); | ||
| try { | ||
| const res = await this.callClient.post<any, any>( | ||
| "/mcp", | ||
| { | ||
| method: "tools/call", | ||
| params: { | ||
| name: toolName, | ||
| arguments: args, | ||
| }, | ||
| jsonrpc: "2.0", | ||
| id: 1, | ||
| }, | ||
| ctx.projectId | ||
| ? { | ||
| headers: { | ||
| "x-goog-user-project": ctx.projectId, | ||
| }, | ||
| } | ||
| : {}, | ||
| ); | ||
| return CallToolResultSchema.parse(res.body.result); | ||
| } catch (error) { | ||
| if (error instanceof FirebaseError) { | ||
| const firebaseError = error; | ||
| const body = (firebaseError.context as any)?.body; | ||
| if (body?.result?.isError) { | ||
| return CallToolResultSchema.parse(body.result); | ||
| } | ||
| } | ||
| throw error; | ||
| } | ||
| } | ||
| } | ||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.