Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 2s
157 lines
4.6 KiB
TypeScript
157 lines
4.6 KiB
TypeScript
import { createMcpResponseBudget, dispatchTool } from './dispatch';
|
|
import type { McpAuth } from './server';
|
|
|
|
const auth: McpAuth = {
|
|
userId: 'user-id',
|
|
authToken: 'auth-token',
|
|
};
|
|
|
|
describe('MCP tool dispatch', () => {
|
|
const originalPort = process.env.PORT;
|
|
const runtimeGlobal = globalThis as typeof globalThis & { __meteor_runtime_config__?: { ROOT_URL_PATH_PREFIX?: string } };
|
|
const originalRuntimeConfig = runtimeGlobal.__meteor_runtime_config__;
|
|
|
|
afterEach(() => {
|
|
jest.restoreAllMocks();
|
|
if (originalPort === undefined) {
|
|
delete process.env.PORT;
|
|
} else {
|
|
process.env.PORT = originalPort;
|
|
}
|
|
if (originalRuntimeConfig === undefined) {
|
|
delete runtimeGlobal.__meteor_runtime_config__;
|
|
} else {
|
|
runtimeGlobal.__meteor_runtime_config__ = originalRuntimeConfig;
|
|
}
|
|
});
|
|
|
|
it('forwards GET arguments and caller identity to the local REST API', async () => {
|
|
process.env.PORT = '3100';
|
|
runtimeGlobal.__meteor_runtime_config__ = { ROOT_URL_PATH_PREFIX: '/chat' };
|
|
const fetchMock = jest
|
|
.spyOn(global, 'fetch')
|
|
.mockResolvedValue(new Response(JSON.stringify({ message: { _id: 'message-id' } }), { status: 200 }));
|
|
|
|
const result = await dispatchTool(
|
|
{
|
|
name: 'chat_getMessage',
|
|
description: 'Get a message',
|
|
inputSchema: { type: 'object' },
|
|
path: '/api/v1/chat.getMessage',
|
|
method: 'get',
|
|
},
|
|
{ msgId: 'message-id', fields: { msg: 1 }, optional: undefined },
|
|
auth,
|
|
'192.0.2.1',
|
|
);
|
|
|
|
expect(fetchMock).toHaveBeenCalledWith(
|
|
'http://127.0.0.1:3100/chat/api/v1/chat.getMessage?msgId=message-id&fields=%7B%22msg%22%3A1%7D',
|
|
expect.objectContaining({
|
|
method: 'GET',
|
|
redirect: 'error',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-User-Id': 'user-id',
|
|
'X-Auth-Token': 'auth-token',
|
|
'X-Real-IP': '192.0.2.1',
|
|
},
|
|
signal: expect.any(AbortSignal),
|
|
}),
|
|
);
|
|
expect(result).toEqual({ ok: true, status: 200, body: { message: { _id: 'message-id' } } });
|
|
});
|
|
|
|
it('preserves non-JSON error responses', async () => {
|
|
jest.spyOn(global, 'fetch').mockResolvedValue(new Response('Service unavailable', { status: 503 }));
|
|
|
|
await expect(
|
|
dispatchTool(
|
|
{
|
|
name: 'chat_postMessage',
|
|
description: 'Post a message',
|
|
inputSchema: { type: 'object' },
|
|
path: '/api/v1/chat.postMessage',
|
|
method: 'post',
|
|
},
|
|
{ roomId: 'room-id', text: 'Hello' },
|
|
auth,
|
|
),
|
|
).resolves.toEqual({ ok: false, status: 503, body: 'Service unavailable' });
|
|
});
|
|
|
|
it('handles empty REST responses', async () => {
|
|
jest.spyOn(global, 'fetch').mockResolvedValue(new Response(null, { status: 204 }));
|
|
|
|
await expect(
|
|
dispatchTool(
|
|
{
|
|
name: 'subscriptions_read',
|
|
description: 'Mark a subscription as read',
|
|
inputSchema: { type: 'object' },
|
|
path: '/api/v1/subscriptions.read',
|
|
method: 'post',
|
|
},
|
|
{ rid: 'room-id' },
|
|
auth,
|
|
),
|
|
).resolves.toEqual({ ok: true, status: 204, body: '' });
|
|
});
|
|
|
|
it('rejects responses whose content length exceeds the MCP result size limit', async () => {
|
|
jest.spyOn(global, 'fetch').mockResolvedValue(
|
|
new Response(null, {
|
|
status: 200,
|
|
headers: { 'content-length': String(5 * 1024 * 1024 + 1) },
|
|
}),
|
|
);
|
|
|
|
await expect(
|
|
dispatchTool(
|
|
{
|
|
name: 'rooms_get',
|
|
description: 'Get rooms',
|
|
inputSchema: { type: 'object' },
|
|
path: '/api/v1/rooms.get',
|
|
method: 'get',
|
|
},
|
|
{},
|
|
auth,
|
|
),
|
|
).rejects.toThrow('MCP tool response exceeds the 5 MiB limit');
|
|
});
|
|
|
|
it('stops streaming responses that exceed the MCP result size limit', async () => {
|
|
jest.spyOn(global, 'fetch').mockResolvedValue(new Response(new Uint8Array(5 * 1024 * 1024 + 1), { status: 200 }));
|
|
|
|
await expect(
|
|
dispatchTool(
|
|
{
|
|
name: 'rooms_get',
|
|
description: 'Get rooms',
|
|
inputSchema: { type: 'object' },
|
|
path: '/api/v1/rooms.get',
|
|
method: 'get',
|
|
},
|
|
{},
|
|
auth,
|
|
),
|
|
).rejects.toThrow('MCP tool response exceeds the 5 MiB limit');
|
|
});
|
|
|
|
it('shares the response size budget across batched tool calls', async () => {
|
|
jest.spyOn(global, 'fetch').mockImplementation(async () => new Response('abc', { status: 200 }));
|
|
const responseBudget = createMcpResponseBudget(5);
|
|
const tool = {
|
|
name: 'rooms_get',
|
|
description: 'Get rooms',
|
|
inputSchema: { type: 'object' },
|
|
path: '/api/v1/rooms.get',
|
|
method: 'get' as const,
|
|
};
|
|
|
|
await expect(dispatchTool(tool, {}, auth, undefined, responseBudget)).resolves.toMatchObject({ body: 'abc' });
|
|
await expect(dispatchTool(tool, {}, auth, undefined, responseBudget)).rejects.toThrow('MCP batch response exceeds the 5 bytes limit');
|
|
});
|
|
});
|