mcp-tool-annotations.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * Read-only MCP ToolAnnotations on every codegraph tool (issue #1018).
  3. *
  4. * Every codegraph tool is query-only — it reads the pre-built index and never
  5. * mutates the workspace. Clients gate on this: Cursor's Ask mode refuses any MCP
  6. * tool that doesn't advertise `readOnlyHint: true`, so without annotations the
  7. * codegraph tools were blocked there even though they only read.
  8. *
  9. * These tests pin that the read-only contract is present on the master tool
  10. * array AND survives every transform that builds a `tools/list` response — the
  11. * static proxy surface (`getStaticTools`), the live surface (`getTools`, which
  12. * rewrites codegraph_explore's description via spread), and the no-default-
  13. * project surface (`withRequiredProjectPath`, which clones the schema). A drop in
  14. * any of those would silently re-block the tools in Ask mode.
  15. */
  16. import { describe, it, expect, afterEach, beforeEach } from 'vitest';
  17. import * as fs from 'fs';
  18. import * as path from 'path';
  19. import * as os from 'os';
  20. import { ToolHandler, getStaticTools, tools, type ToolDefinition } from '../src/mcp/tools';
  21. import { CodeGraph } from '../src';
  22. const ENV = 'CODEGRAPH_MCP_TOOLS';
  23. const ALL_TOOLS = tools.map((t) => t.name).join(',');
  24. /** Assert a single tool advertises the full read-only contract from #1018. */
  25. function expectReadOnly(tool: ToolDefinition): void {
  26. expect(tool.annotations, `${tool.name} is missing annotations`).toBeDefined();
  27. // The hint Cursor Ask mode (and other clients) gate on.
  28. expect(tool.annotations!.readOnlyHint).toBe(true);
  29. // The exact triplet the issue asks for, plus the honest closed-world hint.
  30. expect(tool.annotations!.destructiveHint).toBe(false);
  31. expect(tool.annotations!.idempotentHint).toBe(true);
  32. expect(tool.annotations!.openWorldHint).toBe(false);
  33. }
  34. describe('Read-only annotations on the codegraph MCP tools (#1018)', () => {
  35. const original = process.env[ENV];
  36. afterEach(() => {
  37. if (original === undefined) delete process.env[ENV];
  38. else process.env[ENV] = original;
  39. });
  40. it('every tool in the master array is annotated read-only', () => {
  41. expect(tools.length).toBeGreaterThan(0);
  42. for (const tool of tools) expectReadOnly(tool);
  43. });
  44. it('the static proxy surface carries annotations on every exposed tool', () => {
  45. // getStaticTools() answers tools/list before any project opens (proxy path).
  46. process.env[ENV] = ALL_TOOLS;
  47. const got = getStaticTools();
  48. expect(got.map((t) => t.name).sort()).toEqual(tools.map((t) => t.name).sort());
  49. for (const tool of got) expectReadOnly(tool);
  50. });
  51. it('the no-default-project surface keeps annotations through the schema clone', () => {
  52. // withRequiredProjectPath (null cg) clones each tool's inputSchema — the
  53. // top-level annotations field must ride along on the spread.
  54. process.env[ENV] = ALL_TOOLS;
  55. const got = new ToolHandler(null).getTools();
  56. expect(got.length).toBe(tools.length);
  57. for (const tool of got) {
  58. expectReadOnly(tool);
  59. // Sanity: this IS the clone path (projectPath got marked required).
  60. expect(tool.inputSchema.required ?? []).toContain('projectPath');
  61. }
  62. });
  63. });
  64. describe('Live tool surface keeps annotations with a project open (#1018)', () => {
  65. let tempDir: string;
  66. let cg: CodeGraph;
  67. const original = process.env[ENV];
  68. beforeEach(async () => {
  69. tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-annot-'));
  70. fs.writeFileSync(
  71. path.join(tempDir, 'pay.ts'),
  72. 'export function processPayment(amount: number): boolean { return amount > 0; }\n'
  73. );
  74. cg = await CodeGraph.init(tempDir, { index: true });
  75. });
  76. afterEach(() => {
  77. cg.close();
  78. fs.rmSync(tempDir, { recursive: true, force: true });
  79. if (original === undefined) delete process.env[ENV];
  80. else process.env[ENV] = original;
  81. });
  82. it('getTools() keeps annotations, incl. codegraph_explore whose description is rebuilt', () => {
  83. process.env[ENV] = ALL_TOOLS;
  84. const got = new ToolHandler(cg).getTools();
  85. expect(got.length).toBeGreaterThan(0);
  86. for (const tool of got) expectReadOnly(tool);
  87. // explore's description is regenerated with a per-repo budget suffix via
  88. // object spread; the annotation must survive that rewrite.
  89. const explore = got.find((t) => t.name === 'codegraph_explore');
  90. expect(explore).toBeDefined();
  91. expect(explore!.description).toMatch(/Budget: make at most/);
  92. expectReadOnly(explore!);
  93. });
  94. });