-
Notifications
You must be signed in to change notification settings - Fork 2.2k
fix: register ACP MCP tools as native callable tools via cpython proxy #2002
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5c66a83
fix: register ACP MCP tools as native callable tools via cpython proxy
sethkarten 5210410
fix: add MCP proxy tool names to allowlist so model can see them
sethkarten 4c88721
fix: add missing details field to ACP MCP tool execute results
sethkarten 41985b9
fix(coding-agent): harden ACP MCP proxy lifecycle
sethkarten 2a45748
fix(coding-agent): reject MCP without cpython
sethkarten 36de03e
refactor(coding-agent): narrow generic MCP accessor
sethkarten 2c006d4
fix(coding-agent): cap ACP MCP server names so composed tool names fi…
snimu 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
Some comments aren't visible on the classic Files Changed page.
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 native callable tools for MCP servers supplied by ACP clients. ([#2002](https://github.com/PrimeIntellect-ai/prime-agent/pull/2002)) |
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,99 @@ | ||
| import type { ToolDefinition } from "../extensions/types.js"; | ||
| import type { ExecuteResult } from "../kernel/index.js"; | ||
| import type { AcpMcpServerConfig } from "../mcp/acp-mcp-types.js"; | ||
| import type { IpythonKernelProvisioner } from "./ipython.js"; | ||
|
|
||
| // 48 keeps `mcp_list_tools_<name>` within providers' 64-char tool-name limits. | ||
| const ACP_MCP_SERVER_NAME_PATTERN = /^[A-Za-z0-9][A-Za-z0-9_-]{0,48}$/; | ||
|
|
||
| export function acpMcpToolNames(servers: readonly AcpMcpServerConfig[]): string[] { | ||
| const names: string[] = []; | ||
| const seenServers = new Set<string>(); | ||
| for (const server of servers) { | ||
| if (!ACP_MCP_SERVER_NAME_PATTERN.test(server.name)) { | ||
| throw new Error(`Invalid ACP MCP server name: ${server.name}`); | ||
| } | ||
| if (seenServers.has(server.name)) { | ||
| throw new Error(`Duplicate ACP MCP server: ${server.name}`); | ||
| } | ||
| seenServers.add(server.name); | ||
| names.push(`mcp_list_tools_${server.name}`, `mcp_call_${server.name}`); | ||
| } | ||
| return names; | ||
| } | ||
|
|
||
| function executionResult(result: ExecuteResult) { | ||
| let text = result.stdout; | ||
| if (result.stderr) text += `${text ? "\n" : ""}${result.stderr}`; | ||
| if (result.result) text += `${text ? "\n" : ""}${result.result}`; | ||
| if (result.error) text += `${text ? "\n" : ""}${result.error.traceback.join("\n")}`; | ||
| if (result.status !== "ok") { | ||
| throw new Error(text || `MCP kernel execution ${result.status}`); | ||
| } | ||
| return { | ||
| content: [{ type: "text" as const, text: text || "(empty)" }], | ||
| details: { | ||
| durationMs: result.durationMs, | ||
| status: result.status, | ||
| stdout: result.stdout, | ||
| stderr: result.stderr, | ||
| result: result.result, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| async function executeMcpCode(provisioner: IpythonKernelProvisioner, code: string, signal: AbortSignal | undefined) { | ||
| const manager = await provisioner.ensure(() => {}, signal); | ||
| return executionResult(await manager.execute(code, { signal })); | ||
| } | ||
|
|
||
| export function createAcpMcpToolDefinitions( | ||
| servers: readonly AcpMcpServerConfig[], | ||
| provisioner: IpythonKernelProvisioner, | ||
| ): ToolDefinition[] { | ||
| const names = acpMcpToolNames(servers); | ||
| const definitions: ToolDefinition[] = []; | ||
| for (const [index, server] of servers.entries()) { | ||
| const listToolName = names[index * 2]!; | ||
| const callToolName = names[index * 2 + 1]!; | ||
| const serverName = JSON.stringify(server.name); | ||
|
|
||
| definitions.push({ | ||
| name: listToolName, | ||
| label: `list tools from ${server.name}`, | ||
| description: | ||
| `List every tool the "${server.name}" MCP server exposes. ` + | ||
| `Call this first, then use ${callToolName} to invoke a specific tool.`, | ||
| parameters: { type: "object", properties: {}, required: [], additionalProperties: false }, | ||
| execute: async (_toolCallId, _params, signal, _onUpdate, _ctx) => { | ||
| const code = `print(__import__("json").dumps(await mcp.list_tools(${serverName}), default=str))`; | ||
| return executeMcpCode(provisioner, code, signal); | ||
| }, | ||
| }); | ||
|
|
||
| definitions.push({ | ||
| name: callToolName, | ||
| label: `call tool on ${server.name}`, | ||
| description: | ||
| `Call a tool on the "${server.name}" MCP server. ` + | ||
| `Use ${listToolName} first to discover available tool names and argument schemas.`, | ||
| parameters: { | ||
| type: "object", | ||
| properties: { | ||
| tool: { type: "string", description: `Tool name on "${server.name}".` }, | ||
| arguments: { type: "object", description: "JSON arguments for the tool.", additionalProperties: true }, | ||
| }, | ||
| required: ["tool", "arguments"], | ||
| additionalProperties: false, | ||
| }, | ||
| execute: async (_toolCallId, params, signal, _onUpdate, _ctx) => { | ||
| const { tool, arguments: args } = params as { tool: string; arguments: Record<string, unknown> }; | ||
| const code = | ||
| `print(__import__("json").dumps(await mcp.call_tool(${serverName}, ${JSON.stringify(tool)}, ` + | ||
| `__import__("json").loads(${JSON.stringify(JSON.stringify(args ?? {}))})), default=str))`; | ||
| return executeMcpCode(provisioner, code, signal); | ||
| }, | ||
| }); | ||
| } | ||
| return definitions; | ||
| } |
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
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.