1
0

mcp-tool-allowlist.test.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 full tool surface when unset', () => {
  17. delete process.env[ENV];
  18. const all = listed();
  19. expect(all).toContain('codegraph_explore');
  20. expect(all).toContain('codegraph_context');
  21. expect(all).toContain('codegraph_trace');
  22. expect(all.length).toBeGreaterThanOrEqual(10);
  23. });
  24. it('filters ListTools to the allowlisted short names', () => {
  25. process.env[ENV] = 'trace,search,node';
  26. expect(listed()).toEqual(['codegraph_node', 'codegraph_search', 'codegraph_trace']);
  27. });
  28. it('accepts fully-qualified codegraph_ names and ignores whitespace', () => {
  29. process.env[ENV] = ' codegraph_trace , search ';
  30. expect(listed()).toEqual(['codegraph_search', 'codegraph_trace']);
  31. });
  32. it('treats an empty/whitespace value as unset (full surface)', () => {
  33. process.env[ENV] = ' ';
  34. expect(listed().length).toBeGreaterThanOrEqual(10);
  35. });
  36. it('rejects a disabled tool on execute (defense in depth)', async () => {
  37. process.env[ENV] = 'trace';
  38. const res = await new ToolHandler(null).execute('codegraph_explore', {});
  39. expect(res.isError).toBe(true);
  40. expect(res.content[0].text).toMatch(/disabled via CODEGRAPH_MCP_TOOLS/);
  41. });
  42. it('lets an allowlisted tool past the guard', async () => {
  43. process.env[ENV] = 'search';
  44. // No CodeGraph attached, so it fails *after* the allowlist guard — the
  45. // "disabled" message must NOT appear, proving the guard passed it through.
  46. const res = await new ToolHandler(null).execute('codegraph_search', { query: 'x' });
  47. expect(res.content[0].text).not.toMatch(/disabled via CODEGRAPH_MCP_TOOLS/);
  48. });
  49. });