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
151 lines
4.7 KiB
TypeScript
151 lines
4.7 KiB
TypeScript
import { getCuratedTools, getExtendedTools, type McpTool } from './catalog';
|
|
import { dispatchTool, type McpResponseBudget } from './dispatch';
|
|
import { SUPPORTED_PROTOCOL_VERSIONS } from './transport';
|
|
import { getTrimmedServerVersion } from '../../../../server/api/lib/getTrimmedServerVersion';
|
|
import { settings } from '../../../../server/settings/cached';
|
|
|
|
export type McpAuth = {
|
|
userId: string;
|
|
/** The raw (unhashed) Personal Access Token, forwarded to the REST layer on dispatch. */
|
|
authToken: string;
|
|
};
|
|
|
|
const DEFAULT_PROTOCOL_VERSION = '2025-11-25';
|
|
|
|
const negotiateProtocolVersion = (requestedVersion: unknown): string =>
|
|
typeof requestedVersion === 'string' && SUPPORTED_PROTOCOL_VERSIONS.has(requestedVersion) ? requestedVersion : DEFAULT_PROTOCOL_VERSION;
|
|
|
|
export type JsonRpcRequest = {
|
|
jsonrpc: '2.0';
|
|
id?: string | number;
|
|
method: string;
|
|
params?: Record<string, unknown>;
|
|
};
|
|
|
|
export type JsonRpcResponse = {
|
|
jsonrpc: '2.0';
|
|
id: string | number | null;
|
|
result?: unknown;
|
|
error?: { code: number; message: string; data?: unknown };
|
|
};
|
|
|
|
const result = (id: JsonRpcRequest['id'], value: unknown): JsonRpcResponse => ({ jsonrpc: '2.0', id: id ?? null, result: value });
|
|
|
|
const error = (id: JsonRpcResponse['id'] | undefined, code: number, message: string, data?: unknown): JsonRpcResponse => ({
|
|
jsonrpc: '2.0',
|
|
id: id ?? null,
|
|
error: { code, message, ...(data !== undefined && { data }) },
|
|
});
|
|
|
|
const isRecord = (value: unknown): value is Record<string, unknown> => Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
|
|
const isInitializeParams = (params: unknown): params is Record<string, unknown> => {
|
|
if (!isRecord(params) || typeof params.protocolVersion !== 'string' || !isRecord(params.capabilities) || !isRecord(params.clientInfo)) {
|
|
return false;
|
|
}
|
|
|
|
return typeof params.clientInfo.name === 'string' && typeof params.clientInfo.version === 'string';
|
|
};
|
|
|
|
export const isJsonRpcRequest = (value: unknown): value is JsonRpcRequest => {
|
|
if (!isRecord(value) || value.jsonrpc !== '2.0' || typeof value.method !== 'string' || value.method.length === 0) {
|
|
return false;
|
|
}
|
|
|
|
if (value.id !== undefined && typeof value.id !== 'string' && typeof value.id !== 'number') {
|
|
return false;
|
|
}
|
|
|
|
return value.params === undefined || isRecord(value.params);
|
|
};
|
|
|
|
const listTools = (): McpTool[] => {
|
|
if (settings.get<boolean>('MCP_Expose_Extended_API')) {
|
|
return getExtendedTools();
|
|
}
|
|
return getCuratedTools();
|
|
};
|
|
|
|
const toToolDefinition = ({ name, description, inputSchema }: McpTool) => ({ name, description, inputSchema });
|
|
|
|
/**
|
|
* Handle a single JSON-RPC message. Returns the response object, or `null` for
|
|
* notifications (which must not produce a response per the JSON-RPC spec).
|
|
*/
|
|
export const handleRpcMessage = async (
|
|
message: unknown,
|
|
auth: McpAuth,
|
|
clientIp?: string,
|
|
responseBudget?: McpResponseBudget,
|
|
): Promise<JsonRpcResponse | null> => {
|
|
if (!isJsonRpcRequest(message)) {
|
|
return error(null, -32600, 'Invalid Request');
|
|
}
|
|
|
|
const { id, method, params } = message;
|
|
const respond = (response: JsonRpcResponse): JsonRpcResponse | null => (id === undefined ? null : response);
|
|
|
|
switch (method) {
|
|
case 'initialize': {
|
|
if (!isInitializeParams(params)) {
|
|
return respond(error(id, -32602, 'Invalid initialize parameters'));
|
|
}
|
|
|
|
return respond(
|
|
result(id, {
|
|
protocolVersion: negotiateProtocolVersion(params.protocolVersion),
|
|
capabilities: { tools: { listChanged: false } },
|
|
serverInfo: {
|
|
name: 'rocketchat',
|
|
version: getTrimmedServerVersion(),
|
|
},
|
|
}),
|
|
);
|
|
}
|
|
|
|
case 'notifications/initialized':
|
|
case 'notifications/cancelled':
|
|
return null;
|
|
|
|
case 'ping':
|
|
return respond(result(id, {}));
|
|
|
|
case 'tools/list':
|
|
return respond(result(id, { tools: listTools().map(toToolDefinition) }));
|
|
|
|
case 'tools/call': {
|
|
const name = params?.name;
|
|
const args = params?.arguments;
|
|
if (typeof name !== 'string' || (args !== undefined && !isRecord(args))) {
|
|
return respond(error(id, -32602, 'Invalid tools/call parameters'));
|
|
}
|
|
|
|
const tool = listTools().find((t) => t.name === name);
|
|
|
|
if (!tool) {
|
|
return respond(error(id, -32602, `Unknown tool: ${name}`));
|
|
}
|
|
|
|
try {
|
|
const dispatch = await dispatchTool(tool, args ?? {}, auth, clientIp, responseBudget);
|
|
return respond(
|
|
result(id, {
|
|
content: [{ type: 'text', text: JSON.stringify(dispatch.body) }],
|
|
isError: !dispatch.ok,
|
|
}),
|
|
);
|
|
} catch (err) {
|
|
return respond(
|
|
result(id, {
|
|
content: [{ type: 'text', text: `Tool execution failed: ${err instanceof Error ? err.message : String(err)}` }],
|
|
isError: true,
|
|
}),
|
|
);
|
|
}
|
|
}
|
|
|
|
default:
|
|
return respond(error(id, -32601, `Method not found: ${method}`));
|
|
}
|
|
};
|