1
0

mcp-tool-allowlist.test.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 ONLY codegraph_explore by default when unset', () => {
  17. delete process.env[ENV];
  18. // The default set (see DEFAULT_MCP_TOOLS) is pared to explore alone — the one
  19. // tool that earns its place (verbatim source grouped by file).
  20. // node/search/callers/callees/impact/files/status stay defined and executable
  21. // but unlisted; CODEGRAPH_MCP_TOOLS re-enables them.
  22. expect(listed()).toEqual(['codegraph_explore']);
  23. });
  24. it('re-enables an unlisted tool via the allowlist (impact)', () => {
  25. process.env[ENV] = 'explore,impact';
  26. expect(listed()).toEqual(['codegraph_explore', 'codegraph_impact']);
  27. });
  28. it('filters ListTools to the allowlisted short names', () => {
  29. process.env[ENV] = 'explore,search,node';
  30. expect(listed()).toEqual(['codegraph_explore', 'codegraph_node', 'codegraph_search']);
  31. });
  32. it('accepts fully-qualified codegraph_ names and ignores whitespace', () => {
  33. process.env[ENV] = ' codegraph_explore , search ';
  34. expect(listed()).toEqual(['codegraph_explore', 'codegraph_search']);
  35. });
  36. it('treats an empty/whitespace value as unset (default surface)', () => {
  37. process.env[ENV] = ' ';
  38. expect(listed()).toEqual(['codegraph_explore']);
  39. });
  40. it('rejects a disabled tool on execute (defense in depth)', async () => {
  41. process.env[ENV] = 'node';
  42. const res = await new ToolHandler(null).execute('codegraph_explore', {});
  43. expect(res.isError).toBe(true);
  44. expect(res.content[0].text).toMatch(/disabled via CODEGRAPH_MCP_TOOLS/);
  45. });
  46. it('lets an allowlisted tool past the guard', async () => {
  47. process.env[ENV] = 'search';
  48. // No CodeGraph attached, so it fails *after* the allowlist guard — the
  49. // "disabled" message must NOT appear, proving the guard passed it through.
  50. const res = await new ToolHandler(null).execute('codegraph_search', { query: 'x' });
  51. expect(res.content[0].text).not.toMatch(/disabled via CODEGRAPH_MCP_TOOLS/);
  52. });
  53. });