raiton 6.4.0-beta.12 → 6.5.0-beta.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,4 @@
1
1
  export * from "./body-parser.plugin";
2
2
  export * from "./security/index";
3
- export * from "./openapi.plugin";
3
+ export * from "./openapi.plugin";
4
+ export * from "./mcp.plugin";
@@ -0,0 +1,87 @@
1
+ import type {PluginInterface} from "../../types";
2
+ import type {ContextInterface} from "../../types/core";
3
+ import {PluginScope} from "../../core";
4
+ import {findMcpServerForPath} from "../../core/mcp/metadata";
5
+ import {isMcpJsonRpc, mcpHandleGet, mcpHandlePost, SESSION_HEADER} from "../mcp/streamable-http";
6
+
7
+ export interface McpPluginOptions {
8
+ /**
9
+ * Global prefix for all MCP endpoints.
10
+ * A POST route will be registered at this path.
11
+ * @default '/mcp'
12
+ */
13
+ path?: string;
14
+
15
+ /**
16
+ * Enable the GET endpoint (returns a JSON session handshake).
17
+ * @default false
18
+ */
19
+ enableGet?: boolean;
20
+ }
21
+
22
+ export function mcpPlugin(options: McpPluginOptions = {}): PluginInterface {
23
+ const {
24
+ path = '/mcp',
25
+ enableGet = false,
26
+ } = options;
27
+
28
+ return {
29
+ name: 'mcp-plugin',
30
+ setup: (scope: PluginScope) => {
31
+
32
+ scope.route('POST', path, async (context: ContextInterface) => {
33
+ const entry = findMcpServerForPath(path, undefined)
34
+ if (!entry) {
35
+ return {error: {code: -32002, message: 'MCP server not registered at this path'}}
36
+ }
37
+
38
+ const existingSessionId = readSessionId(context)
39
+
40
+ if (!isMcpJsonRpc(context.req.body)) {
41
+ return {error: {code: -32700, message: 'Invalid JSON-RPC request'}}
42
+ }
43
+
44
+ const result = await mcpHandlePost(entry, context.req.body, existingSessionId)
45
+ context.reply.status(result.status)
46
+ if (result.headers[SESSION_HEADER]) {
47
+ context.reply.header(SESSION_HEADER, result.headers[SESSION_HEADER])
48
+ }
49
+ if (result.body === '') {
50
+ context.reply.send('')
51
+ return
52
+ }
53
+ return result.body
54
+ })
55
+
56
+ if (enableGet) {
57
+ scope.route('GET', path, async (context: ContextInterface) => {
58
+ const entry = findMcpServerForPath(path, undefined)
59
+ if (!entry) {
60
+ return {error: {code: -32002, message: 'MCP server not registered at this path'}}
61
+ }
62
+
63
+ const existingSessionId = readSessionId(context)
64
+ const result = mcpHandleGet(entry, existingSessionId)
65
+ context.reply.status(result.status)
66
+ if (result.headers[SESSION_HEADER]) {
67
+ context.reply.header(SESSION_HEADER, result.headers[SESSION_HEADER])
68
+ }
69
+ return result.body
70
+ })
71
+ }
72
+ }
73
+ }
74
+ }
75
+
76
+ function readSessionId(context: any): string | undefined {
77
+ const req = context.req
78
+ if (req?.headers?.get) {
79
+ const value = req.headers.get(SESSION_HEADER)
80
+ if (value) return value
81
+ }
82
+ if (req?.headers && typeof req.headers === 'object') {
83
+ const value = (req.headers as any)[SESSION_HEADER]
84
+ if (value) return value
85
+ }
86
+ return undefined
87
+ }
@@ -10,6 +10,10 @@ export function isServiceArtifact(filename: string) {
10
10
  return isArtifact(filename, 'service')
11
11
  }
12
12
 
13
+ export function isMcpArtifact(filename: string) {
14
+ return isArtifact(filename, 'mcp')
15
+ }
16
+
13
17
  export function isArtifact(filename: string, artifact: string) {
14
18
  return [
15
19
  filename.endsWith(`.${artifact}.ts`),
@@ -3,6 +3,7 @@ import {METADATA_KEYS, Parametrable} from "../index";
3
3
  import {PluginScope} from "../../core";
4
4
  import {RaitonConfig} from "../../core/config";
5
5
  import {getSocketRegistry} from "../../core/socket";
6
+ import {getMcpServerRegistry} from "../../core/mcp";
6
7
  import {RaitonThread} from "../../core/thread";
7
8
 
8
9
  /**
@@ -413,6 +414,86 @@ export async function generateOpenApiSpec(scope: PluginScope, opts: any): Promis
413
414
  }
414
415
  }
415
416
 
417
+ // Document registered MCP servers as streamable HTTP (POST JSON-RPC) operations
418
+ const mcpServers = getMcpServerRegistry();
419
+
420
+ for (const [, mcpEntry] of mcpServers) {
421
+ const mcpPath = mcpEntry.path;
422
+ if (!spec.paths[mcpPath]) {
423
+ spec.paths[mcpPath] = {};
424
+ }
425
+
426
+ const elements = [
427
+ ...mcpEntry.metadata.tools.map(t => ({kind: 'tool', ...t})),
428
+ ...mcpEntry.metadata.prompts.map(p => ({kind: 'prompt', ...p})),
429
+ ...mcpEntry.metadata.resources.map(r => ({kind: 'resource', ...r})),
430
+ ...mcpEntry.metadata.resourceTemplates.map(r => ({kind: 'resource-template', ...r})),
431
+ ];
432
+
433
+ (spec.paths[mcpPath] as any).post = {
434
+ summary: `MCP server: ${mcpEntry.name} (${mcpEntry.version})`,
435
+ description: mcpEntry.metadata.description
436
+ || `Model Context Protocol server. Exposes ${
437
+ elements.map(e => `${e.kind}s`).filter((v, i, a) => a.indexOf(v) === i).join(', ') || 'no elements'
438
+ }.`,
439
+ parameters: [],
440
+ requestBody: {
441
+ required: true,
442
+ content: {
443
+ 'application/json': {
444
+ schema: {
445
+ type: 'object',
446
+ properties: {
447
+ jsonrpc: {type: 'string', const: '2.0'},
448
+ id: {type: ['number', 'string', 'null']},
449
+ method: {type: 'string', enum: [...new Set(elements.map(e => e.kind === 'tool' ? 'tools/call' : e.kind === 'prompt' ? 'prompts/get' : 'resources/read'))]},
450
+ params: {type: 'object'}
451
+ },
452
+ required: ['jsonrpc', 'method']
453
+ }
454
+ }
455
+ }
456
+ },
457
+ responses: {
458
+ '200': {
459
+ description: 'Successful JSON-RPC response',
460
+ content: {'application/json': {schema: {type: 'object'}}}
461
+ },
462
+ '202': {
463
+ description: 'Request accepted (notification)'
464
+ },
465
+ '406': {
466
+ description: 'Unsupported protocol version'
467
+ }
468
+ },
469
+ 'x-mcp': {
470
+ name: mcpEntry.name,
471
+ version: mcpEntry.version,
472
+ transport: 'streamable-http',
473
+ tools: mcpEntry.metadata.tools.map(t => ({
474
+ name: t.name,
475
+ description: t.description,
476
+ inputSchema: {
477
+ type: 'object',
478
+ properties: Object.fromEntries(
479
+ t.arguments.map(a => [a.name, {type: a.schema?.type || 'string', description: a.description}])
480
+ ),
481
+ ...(t.arguments.some(a => a.required) ? {required: t.arguments.filter(a => a.required).map(a => a.name)} : {})
482
+ }
483
+ })),
484
+ prompts: mcpEntry.metadata.prompts.map(p => ({
485
+ name: p.name,
486
+ description: p.description,
487
+ arguments: p.arguments.map(a => ({name: a.name, description: a.description, required: a.required}))
488
+ })),
489
+ resources: [
490
+ ...mcpEntry.metadata.resources.map(r => ({uri: r.uri, name: r.name, mimeType: r.mimeType})),
491
+ ...mcpEntry.metadata.resourceTemplates.map(r => ({uriTemplate: r.uri, name: r.name, mimeType: r.mimeType})),
492
+ ]
493
+ }
494
+ };
495
+ }
496
+
416
497
  return spec;
417
498
  }
418
499
 
@@ -21,4 +21,5 @@ export type * from "./injection"
21
21
  export type * from "./responses"
22
22
  export type * from "./server"
23
23
  export type * from "./raiton"
24
- export type * from "./lifecycle"
24
+ export type * from "./lifecycle"
25
+ export type * from "./mcp"
@@ -0,0 +1,118 @@
1
+ export type McpArgumentType = 'string' | 'number' | 'boolean' | 'integer' | 'array' | 'object' | 'null';
2
+
3
+ export interface McpArgumentSchema {
4
+ type?: McpArgumentType;
5
+ description?: string;
6
+ required?: boolean;
7
+ enum?: any[];
8
+ default?: any;
9
+ items?: McpArgumentSchema;
10
+ properties?: Record<string, McpArgumentSchema>;
11
+ }
12
+
13
+ export interface McpArgumentMetaInterface {
14
+ name: string;
15
+ description?: string;
16
+ schema?: McpArgumentSchema;
17
+ required?: boolean;
18
+ metatype?: any;
19
+ }
20
+
21
+ export type McpElementType = 'tool' | 'prompt' | 'resource' | 'resource-template' | 'read';
22
+
23
+ export interface McpElementMetaInterface {
24
+ type: McpElementType;
25
+ name: string;
26
+ description?: string;
27
+ propertyKey: string;
28
+ arguments: McpArgumentMetaInterface[];
29
+ uri?: string;
30
+ mimeType?: string;
31
+ title?: string;
32
+ }
33
+
34
+ export interface McpServerMetaInterface {
35
+ name?: string;
36
+ version?: string;
37
+ description?: string;
38
+ path: string;
39
+ instructions?: string;
40
+ tools: McpElementMetaInterface[];
41
+ prompts: McpElementMetaInterface[];
42
+ resources: McpElementMetaInterface[];
43
+ resourceTemplates: McpElementMetaInterface[];
44
+ }
45
+
46
+ export interface McpServerOptionsInterface {
47
+ name?: string;
48
+ version?: string;
49
+ description?: string;
50
+ path?: string;
51
+ instructions?: string;
52
+ }
53
+
54
+ export type McpNodeMethod =
55
+ | 'initialize'
56
+ | 'ping'
57
+ | 'tools/list'
58
+ | 'tools/call'
59
+ | 'prompts/list'
60
+ | 'prompts/get'
61
+ | 'resources/list'
62
+ | 'resources/templates/list'
63
+ | 'resources/read'
64
+ | 'logging/setLevel';
65
+
66
+ export type McpTransportType = 'streamable-http' | 'stdio';
67
+
68
+ export interface McpServerRegistrationInterface {
69
+ path: string;
70
+ name: string;
71
+ version: string;
72
+ description?: string;
73
+ construct: any;
74
+ metadata: McpServerMetaInterface;
75
+ }
76
+
77
+ export interface McpRouteDescriptorInterface {
78
+ path: string;
79
+ propertyKey: string;
80
+ serverName: string;
81
+ }
82
+
83
+ export interface JsonRpcRequest {
84
+ jsonrpc?: string;
85
+ method: string;
86
+ params?: any;
87
+ id?: number | string | null;
88
+ }
89
+
90
+ export interface JsonRpcError {
91
+ code: number;
92
+ message: string;
93
+ data?: any;
94
+ }
95
+
96
+ export interface JsonRpcResponse {
97
+ jsonrpc: string;
98
+ id: number | string | null;
99
+ result?: any;
100
+ error?: JsonRpcError;
101
+ }
102
+
103
+ export type McpArgumentDecorator =
104
+ | string
105
+ | McpArgumentMetaInterface;
106
+
107
+ export interface McpToolResultContent {
108
+ type: 'text' | 'image' | 'resource';
109
+ text?: string;
110
+ data?: string;
111
+ mimeType?: string;
112
+ resource?: any;
113
+ }
114
+
115
+ export interface McpTextContent {
116
+ type: 'text';
117
+ text: string;
118
+ }
@@ -9,4 +9,5 @@ export interface RaitonSignalMapInterface {
9
9
  'hmr:socket': BuilderHMRDeclarationInterface;
10
10
  'hmr:middleware': BuilderHMRDeclarationInterface;
11
11
  'hmr:hook': BuilderHMRDeclarationInterface;
12
+ 'hmr:mcp': BuilderHMRDeclarationInterface;
12
13
  }