connection-pool-server-override.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { beforeEach, describe, expect, it, vi } from 'vitest';
  2. import { McpConnectionPool } from '../src/runtime/connection-pool.js';
  3. const state = vi.hoisted(() => ({
  4. transports: [] as unknown[],
  5. clients: [] as Array<{ transport?: unknown }>,
  6. }));
  7. vi.mock('../src/runtime/transport/factory.js', () => ({
  8. createTransport: vi.fn((transport: unknown) => {
  9. const created = { transport, close: vi.fn(async () => undefined) };
  10. state.transports.push(created);
  11. return created;
  12. }),
  13. }));
  14. vi.mock('@modelcontextprotocol/sdk/client/index.js', () => ({
  15. Client: class {
  16. transport?: unknown;
  17. constructor() {
  18. state.clients.push(this);
  19. }
  20. setRequestHandler(): void {}
  21. async connect(transport: unknown): Promise<void> {
  22. this.transport = transport;
  23. }
  24. async listTools(): Promise<{ tools: Array<{ name: string; inputSchema: object }> }> {
  25. return { tools: [{ name: 'ping', inputSchema: {} }] };
  26. }
  27. async close(): Promise<void> {}
  28. },
  29. }));
  30. describe('McpConnectionPool session server override', () => {
  31. beforeEach(() => {
  32. state.transports.length = 0;
  33. state.clients.length = 0;
  34. });
  35. it('connects from the ephemeral descriptor and isolates connections by explicit key', async () => {
  36. const pool = new McpConnectionPool({ getResolvedServer: () => null });
  37. const descriptor = {
  38. name: 'client-tools',
  39. enabled: true,
  40. transport: {
  41. type: 'stdio' as const,
  42. command: process.execPath,
  43. args: ['server.mjs'],
  44. },
  45. };
  46. await pool.listTools('client-tools', {
  47. connectionKey: 'acp-session:one:client-tools',
  48. serverOverride: descriptor,
  49. });
  50. await pool.listTools('client-tools', {
  51. connectionKey: 'acp-session:two:client-tools',
  52. serverOverride: descriptor,
  53. });
  54. expect(state.transports).toHaveLength(2);
  55. expect(state.clients).toHaveLength(2);
  56. expect(state.transports).toEqual([
  57. expect.objectContaining({ transport: descriptor.transport }),
  58. expect.objectContaining({ transport: descriptor.transport }),
  59. ]);
  60. await pool.shutdown();
  61. });
  62. it('separates raw server names from another server scope and disconnects only that server', async () => {
  63. const pool = new McpConnectionPool({
  64. getResolvedServer: (name) => ({
  65. name,
  66. enabled: true,
  67. transport: { type: 'http', url: `https://example.com/${name}` },
  68. }),
  69. });
  70. try {
  71. await pool.listTools('docs:scope');
  72. await pool.listTools('docs', { connectionKey: 'scope' });
  73. expect(state.clients).toHaveLength(2);
  74. await pool.disconnect('docs:scope');
  75. await pool.listTools('docs', { connectionKey: 'scope' });
  76. expect(state.clients).toHaveLength(2);
  77. await pool.listTools('docs:scope');
  78. expect(state.clients).toHaveLength(3);
  79. } finally {
  80. await pool.shutdown();
  81. }
  82. });
  83. it('does not mistake a JSON-shaped raw server for an encoded key during disconnect', async () => {
  84. const pool = new McpConnectionPool({
  85. getResolvedServer: (name) => ({
  86. name,
  87. enabled: true,
  88. transport: { type: 'http', url: 'https://example.com' },
  89. }),
  90. });
  91. try {
  92. await pool.listTools('docs', { connectionKey: 'scope' });
  93. await pool.disconnect('["docs","scope"]');
  94. await pool.listTools('docs', { connectionKey: 'scope' });
  95. expect(state.clients).toHaveLength(1);
  96. await pool.listTools('["docs","scope"]');
  97. expect(state.clients).toHaveLength(2);
  98. await pool.disconnect('["docs","scope"]');
  99. await pool.listTools('docs', { connectionKey: 'scope' });
  100. expect(state.clients).toHaveLength(2);
  101. } finally {
  102. await pool.shutdown();
  103. }
  104. });
  105. it('keeps relative and once-prefixed scopes equivalent without merging an explicit empty scope', async () => {
  106. const pool = new McpConnectionPool({
  107. getResolvedServer: (name) => ({
  108. name,
  109. enabled: true,
  110. transport: { type: 'http', url: 'https://example.com' },
  111. }),
  112. });
  113. try {
  114. await pool.listTools('docs');
  115. await pool.listTools('docs', { connectionKey: '' });
  116. expect(state.clients).toHaveLength(1);
  117. await pool.listTools('docs', { connectionKey: 'scope' });
  118. await pool.listTools('docs', { connectionKey: 'docs:scope' });
  119. expect(state.clients).toHaveLength(2);
  120. await pool.listTools('docs', { connectionKey: 'docs:' });
  121. expect(state.clients).toHaveLength(3);
  122. await pool.listTools('docs', { connectionKey: 'docs:docs:scope' });
  123. expect(state.clients).toHaveLength(4);
  124. await pool.disconnect('docs', { connectionKey: 'docs:scope' });
  125. await pool.listTools('docs');
  126. await pool.listTools('docs', { connectionKey: 'docs:' });
  127. expect(state.clients).toHaveLength(4);
  128. await pool.listTools('docs', { connectionKey: 'scope' });
  129. expect(state.clients).toHaveLength(5);
  130. } finally {
  131. await pool.shutdown();
  132. }
  133. });
  134. });