|
| 1 | +import { describe, expect, it } from "bun:test"; |
| 2 | +import type { CustomToolContext } from "@oh-my-pi/pi-coding-agent/extensibility/custom-tools"; |
| 3 | +import { DeferredMCPTool, MCPTool, type MCPToolDefinition } from "@oh-my-pi/pi-coding-agent/mcp"; |
| 4 | +import type { MCPServerConnection } from "@oh-my-pi/pi-coding-agent/mcp/types"; |
| 5 | +import { createMockConnection, createMockTransport } from "./mcp-test-utils"; |
| 6 | + |
| 7 | +type CapturedRequest = { |
| 8 | + method: string; |
| 9 | + params: Record<string, unknown> | undefined; |
| 10 | +}; |
| 11 | + |
| 12 | +const unusedContext = {} as CustomToolContext; |
| 13 | + |
| 14 | +function createSearchToolDefinition(): MCPToolDefinition { |
| 15 | + return { |
| 16 | + name: "search", |
| 17 | + description: "Search symbols or file locations", |
| 18 | + inputSchema: { |
| 19 | + type: "object", |
| 20 | + properties: { |
| 21 | + symbol: { type: "string" }, |
| 22 | + language: { type: "string" }, |
| 23 | + file: { type: "string" }, |
| 24 | + line: { type: "number" }, |
| 25 | + column: { type: "number" }, |
| 26 | + filters: { type: "object" }, |
| 27 | + exact: { type: "boolean" }, |
| 28 | + }, |
| 29 | + required: ["symbol", "language"], |
| 30 | + }, |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +function createCapturedConnection(calls: CapturedRequest[]): MCPServerConnection { |
| 35 | + const transport = createMockTransport( |
| 36 | + new Map([["tools/call", [{ content: [{ type: "text", text: "ok" }] }]]]), |
| 37 | + (method, params) => calls.push({ method, params }), |
| 38 | + ); |
| 39 | + return createMockConnection({ tools: {} }, transport); |
| 40 | +} |
| 41 | + |
| 42 | +describe("MCP tool arguments", () => { |
| 43 | + it("omits optional empty placeholders before tools/call", async () => { |
| 44 | + const calls: CapturedRequest[] = []; |
| 45 | + const tool = new MCPTool(createCapturedConnection(calls), createSearchToolDefinition()); |
| 46 | + |
| 47 | + await tool.execute( |
| 48 | + "call-1", |
| 49 | + { symbol: "Foo", language: "", file: "", line: 0, filters: {}, exact: false }, |
| 50 | + undefined, |
| 51 | + unusedContext, |
| 52 | + undefined, |
| 53 | + ); |
| 54 | + |
| 55 | + expect(calls).toEqual([ |
| 56 | + { |
| 57 | + method: "tools/call", |
| 58 | + params: { |
| 59 | + name: "search", |
| 60 | + arguments: { symbol: "Foo", language: "", line: 0, exact: false }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + ]); |
| 64 | + }); |
| 65 | + |
| 66 | + it("omits optional empty placeholders for deferred MCP tools", async () => { |
| 67 | + const calls: CapturedRequest[] = []; |
| 68 | + const connection = createCapturedConnection(calls); |
| 69 | + const tool = new DeferredMCPTool("intellij-index", createSearchToolDefinition(), async () => connection); |
| 70 | + |
| 71 | + await tool.execute( |
| 72 | + "call-1", |
| 73 | + { symbol: "Foo", language: "TypeScript", file: "", column: "", filters: {} }, |
| 74 | + undefined, |
| 75 | + unusedContext, |
| 76 | + undefined, |
| 77 | + ); |
| 78 | + |
| 79 | + expect(calls).toEqual([ |
| 80 | + { |
| 81 | + method: "tools/call", |
| 82 | + params: { |
| 83 | + name: "search", |
| 84 | + arguments: { symbol: "Foo", language: "TypeScript" }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + ]); |
| 88 | + }); |
| 89 | +}); |
0 commit comments