mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-17 21:43:06 +00:00
🏓 fix: Preserve MCP Server Ping Responses (#18020)
Preserve valid empty MCP responses to immediate and repeated server pings. Keep client liveness probing unchanged. Fixes #17568.
This commit is contained in:
parent
472d796c20
commit
413456941c
2 changed files with 74 additions and 8 deletions
74
packages/api/src/mcp/__tests__/MCPConnectionPing.test.ts
Normal file
74
packages/api/src/mcp/__tests__/MCPConnectionPing.test.ts
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
||||
import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js';
|
||||
import {
|
||||
McpError,
|
||||
ErrorCode,
|
||||
EmptyResultSchema,
|
||||
PingRequestSchema,
|
||||
ListToolsRequestSchema,
|
||||
} from '@modelcontextprotocol/sdk/types.js';
|
||||
import { MCPConnection } from '~/mcp/connection';
|
||||
|
||||
jest.mock('@librechat/data-schemas', () => ({
|
||||
logger: { info: jest.fn(), warn: jest.fn(), error: jest.fn(), debug: jest.fn() },
|
||||
}));
|
||||
|
||||
jest.mock('~/auth', () => ({
|
||||
createSSRFSafeUndiciConnect: jest.fn(() => undefined),
|
||||
isOAuthUrlAllowed: jest.fn(() => false),
|
||||
isSSRFTarget: jest.fn(() => false),
|
||||
resolveHostnameSSRF: jest.fn(async () => false),
|
||||
}));
|
||||
|
||||
describe('MCPConnection ping responses', () => {
|
||||
let server: Server;
|
||||
let connection: MCPConnection;
|
||||
|
||||
beforeEach(async () => {
|
||||
server = new Server({ name: 'ping-server', version: '1.0.0' }, { capabilities: { tools: {} } });
|
||||
server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [] }));
|
||||
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
|
||||
await server.connect(serverTransport);
|
||||
connection = new MCPConnection({
|
||||
serverName: 'ping-server',
|
||||
serverConfig: { type: 'streamable-http', url: 'http://localhost/mcp' },
|
||||
useSSRFProtection: false,
|
||||
});
|
||||
connection['transport'] = clientTransport;
|
||||
connection['patchTransportSend']();
|
||||
await connection.client.connect(clientTransport);
|
||||
connection.emit('connectionChange', 'connected');
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await connection.client.close();
|
||||
await server.close();
|
||||
});
|
||||
|
||||
it('answers immediate and repeated server pings and keeps ordinary requests usable', async () => {
|
||||
for (let i = 0; i < 3; i++) {
|
||||
await expect(
|
||||
server.request({ method: 'ping' }, EmptyResultSchema, { timeout: 500 }),
|
||||
).resolves.toEqual({});
|
||||
}
|
||||
await expect(connection.client.listTools()).resolves.toEqual({ tools: [] });
|
||||
await expect(connection.isConnected()).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it('still falls back to advertised capabilities when client ping is unsupported', async () => {
|
||||
server.setRequestHandler(PingRequestSchema, async () => {
|
||||
throw new McpError(ErrorCode.MethodNotFound, 'Unsupported method: ping');
|
||||
});
|
||||
const listTools = jest.fn(async () => ({ tools: [] }));
|
||||
server.setRequestHandler(ListToolsRequestSchema, listTools);
|
||||
await expect(connection.isConnected()).resolves.toBe(true);
|
||||
expect(listTools).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('still reports an ordinary client ping failure', async () => {
|
||||
server.setRequestHandler(PingRequestSchema, async () => {
|
||||
throw new McpError(ErrorCode.InternalError, 'Ping unavailable');
|
||||
});
|
||||
await expect(connection.isConnected()).resolves.toBe(false);
|
||||
});
|
||||
});
|
||||
|
|
@ -1124,7 +1124,6 @@ export class MCPConnection extends EventEmitter {
|
|||
private reportedStandaloneSseConflict = false;
|
||||
private agents: Dispatcher[] = [];
|
||||
private readonly userId?: string;
|
||||
private lastPingTime: number;
|
||||
private lastConnectionCheckAt: number = 0;
|
||||
private lastConnectionCheckError?: unknown;
|
||||
private lastConnectionCheckCredentialSetId?: string | null;
|
||||
|
|
@ -1321,7 +1320,6 @@ export class MCPConnection extends EventEmitter {
|
|||
this.iconPath = params.serverConfig.iconPath;
|
||||
this.timeout = params.serverConfig.timeout;
|
||||
this.sseReadTimeout = params.serverConfig.sseReadTimeout;
|
||||
this.lastPingTime = Date.now();
|
||||
this.createdAt = Date.now(); // Record creation timestamp for staleness detection
|
||||
if (params.oauthTokens) {
|
||||
this.oauthTokens = params.oauthTokens;
|
||||
|
|
@ -2505,12 +2503,6 @@ export class MCPConnection extends EventEmitter {
|
|||
|
||||
const originalSend = this.transport.send.bind(this.transport);
|
||||
this.transport.send = async (msg) => {
|
||||
if ('result' in msg && !('method' in msg) && Object.keys(msg.result ?? {}).length === 0) {
|
||||
if (Date.now() - this.lastPingTime < FIVE_MINUTES) {
|
||||
throw new Error('Empty result');
|
||||
}
|
||||
this.lastPingTime = Date.now();
|
||||
}
|
||||
const method = 'method' in msg ? msg.method : undefined;
|
||||
const id = 'id' in msg ? msg.id : undefined;
|
||||
if (
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue