mcp-tool-allowlist.test.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * CODEGRAPH_MCP_TOOLS allowlist — lets an operator (or an A/B harness) trim the
  3. * exposed MCP tool surface without touching the client config. Inert when unset.
  4. * Filtering happens in ListTools (getTools) and is enforced again on execute().
  5. */
  6. import { describe, it, expect, afterEach } from 'vitest';
  7. import { ToolHandler } from '../src/mcp/tools';
  8. const ENV = 'CODEGRAPH_MCP_TOOLS';
  9. describe('CODEGRAPH_MCP_TOOLS allowlist', () => {
  10. const original = process.env[ENV];
  11. afterEach(() => {
  12. if (original === undefined) delete process.env[ENV];
  13. else process.env[ENV] = original;
  14. });
  15. const listed = () => new ToolHandler(null).getTools().map(t => t.name).sort();
  16. it('exposes the default 4-tool surface when unset', () => {
  17. delete process.env[ENV];
  18. // The default set (see DEFAULT_MCP_TOOLS): explore + node are the
  19. // validated workhorses, search the cheap lookup, callers the one
  20. // irreplaceable enumerator. callees/impact/files/status stay defined
  21. // and executable but unlisted — impact appeared in ZERO recorded runs.
  22. expect(listed()).toEqual([
  23. 'codegraph_callers',
  24. 'codegraph_explore',
  25. 'codegraph_node',
  26. 'codegraph_search',
  27. ]);
  28. });
  29. it('re-enables an unlisted tool via the allowlist (impact)', () => {
  30. process.env[ENV] = 'explore,impact';
  31. expect(listed()).toEqual(['codegraph_explore', 'codegraph_impact']);
  32. });
  33. it('filters ListTools to the allowlisted short names', () => {
  34. process.env[ENV] = 'explore,search,node';
  35. expect(listed()).toEqual(['codegraph_explore', 'codegraph_node', 'codegraph_search']);
  36. });
  37. it('accepts fully-qualified codegraph_ names and ignores whitespace', () => {
  38. process.env[ENV] = ' codegraph_explore , search ';
  39. expect(listed()).toEqual(['codegraph_explore', 'codegraph_search']);
  40. });
  41. it('treats an empty/whitespace value as unset (default surface)', () => {
  42. process.env[ENV] = ' ';
  43. expect(listed()).toHaveLength(4);
  44. expect(listed()).toContain('codegraph_explore');
  45. });
  46. it('rejects a disabled tool on execute (defense in depth)', async () => {
  47. process.env[ENV] = 'node';
  48. const res = await new ToolHandler(null).execute('codegraph_explore', {});
  49. expect(res.isError).toBe(true);
  50. expect(res.content[0].text).toMatch(/disabled via CODEGRAPH_MCP_TOOLS/);
  51. });
  52. it('lets an allowlisted tool past the guard', async () => {
  53. process.env[ENV] = 'search';
  54. // No CodeGraph attached, so it fails *after* the allowlist guard — the
  55. // "disabled" message must NOT appear, proving the guard passed it through.
  56. const res = await new ToolHandler(null).execute('codegraph_search', { query: 'x' });
  57. expect(res.content[0].text).not.toMatch(/disabled via CODEGRAPH_MCP_TOOLS/);
  58. });
  59. });