raiton 6.3.1-beta.11 → 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.
- package/CHANGELOG.md +17 -0
- package/build/bin/index.mjs +20011 -1770
- package/docs/user-guide/README.md +17 -12
- package/docs/user-guide/decorators/README.md +2 -0
- package/docs/user-guide/decorators/mcp.md +70 -0
- package/docs/user-guide/https.md +337 -0
- package/docs/user-guide/mcp.md +120 -0
- package/docs/user-guide/plugins.md +1 -0
- package/docs/user-guide/security.md +2 -2
- package/package.json +1 -1
- package/source/core/application.ts +24 -3
- package/source/core/builder.ts +12 -0
- package/source/core/controller/builder.ts +8 -2
- package/source/core/index.ts +1 -0
- package/source/core/mcp/builder.ts +76 -0
- package/source/core/mcp/index.ts +3 -0
- package/source/core/mcp/metadata.ts +80 -0
- package/source/core/mcp/tracker.ts +28 -0
- package/source/core/thread.ts +19 -3
- package/source/framework/artifacts.ts +30 -0
- package/source/framework/constants/decorators.constant.ts +4 -0
- package/source/framework/decorators/index.ts +2 -1
- package/source/framework/decorators/mcp.decorator.ts +170 -0
- package/source/framework/index.ts +1 -0
- package/source/framework/mcp/handler.ts +369 -0
- package/source/framework/mcp/index.ts +3 -0
- package/source/framework/mcp/protocol.ts +50 -0
- package/source/framework/mcp/streamable-http.ts +94 -0
- package/source/framework/plugins/index.ts +2 -1
- package/source/framework/plugins/mcp.plugin.ts +87 -0
- package/source/framework/runtime/bun/server.ts +13 -2
- package/source/framework/runtime/node/server.ts +19 -4
- package/source/framework/utilities/artifact.util.ts +4 -0
- package/source/framework/utilities/https.ts +298 -0
- package/source/framework/utilities/index.ts +2 -1
- package/source/framework/utilities/openapi.utils.ts +112 -13
- package/source/types/application.ts +4 -0
- package/source/types/index.ts +2 -1
- package/source/types/mcp.ts +118 -0
- package/source/types/raiton.ts +1 -0
- package/source/types/runtime.ts +8 -0
|
@@ -8,9 +8,11 @@ import {Logger} from "@protorians/logger";
|
|
|
8
8
|
import {RaitonConfig} from "./config";
|
|
9
9
|
import {Artifacts} from "../framework/artifacts";
|
|
10
10
|
import {Injection} from "./injection";
|
|
11
|
+
import {HttpsConfigInterface, resolveHttpsConfig} from "../framework/utilities/https";
|
|
11
12
|
|
|
12
13
|
export class Application implements ApplicationInterface {
|
|
13
14
|
private root: PluginScope
|
|
15
|
+
private _resolvedHttps: HttpsConfigInterface | undefined
|
|
14
16
|
|
|
15
17
|
readonly version: string = RaitonConfig.get('version') || '0.0.1'
|
|
16
18
|
|
|
@@ -22,6 +24,7 @@ export class Application implements ApplicationInterface {
|
|
|
22
24
|
readonly config: ApplicationConfigInterface
|
|
23
25
|
) {
|
|
24
26
|
this.root = new PluginScope()
|
|
27
|
+
this._resolvedHttps = resolveHttpsConfig(this.config.https)
|
|
25
28
|
if (this.config.workdir) {
|
|
26
29
|
process.chdir(this.config.workdir)
|
|
27
30
|
}
|
|
@@ -41,8 +44,12 @@ export class Application implements ApplicationInterface {
|
|
|
41
44
|
}
|
|
42
45
|
|
|
43
46
|
public get hostname(): string {
|
|
47
|
+
const protocol = this._resolvedHttps?.enabled
|
|
48
|
+
? 'https'
|
|
49
|
+
: (this.config.protocole || 'http')
|
|
50
|
+
|
|
44
51
|
return `${
|
|
45
|
-
|
|
52
|
+
protocol
|
|
46
53
|
}://${
|
|
47
54
|
this.config.hostname || 'localhost'
|
|
48
55
|
}${
|
|
@@ -52,6 +59,10 @@ export class Application implements ApplicationInterface {
|
|
|
52
59
|
}`
|
|
53
60
|
}
|
|
54
61
|
|
|
62
|
+
public get https(): HttpsConfigInterface | undefined {
|
|
63
|
+
return this._resolvedHttps
|
|
64
|
+
}
|
|
65
|
+
|
|
55
66
|
public setOption<K extends keyof ApplicationConfigInterface>(key: K, value: ApplicationConfigInterface[K]): this {
|
|
56
67
|
this.config[key] = value;
|
|
57
68
|
return this;
|
|
@@ -73,8 +84,7 @@ export class Application implements ApplicationInterface {
|
|
|
73
84
|
}
|
|
74
85
|
|
|
75
86
|
route(method: HttpMethod, path: string, handler: RouteHandlerCallable, version?: string): this {
|
|
76
|
-
const
|
|
77
|
-
const fullPath = `${prefix}${path}`.replace(/\/+/g, '/') || '/'
|
|
87
|
+
const fullPath = path.replace(/\/+/g, '/') || '/'
|
|
78
88
|
this.root.route(method, fullPath, handler, version)
|
|
79
89
|
return this
|
|
80
90
|
}
|
|
@@ -130,6 +140,17 @@ export class Application implements ApplicationInterface {
|
|
|
130
140
|
const url = new URL(req.url, this.hostname)
|
|
131
141
|
let pathname = url.pathname
|
|
132
142
|
|
|
143
|
+
if (this.config.prefix) {
|
|
144
|
+
const prefix = this.config.prefix.endsWith('/') ? this.config.prefix.slice(0, -1) : this.config.prefix
|
|
145
|
+
if (pathname === prefix) {
|
|
146
|
+
pathname = '/'
|
|
147
|
+
} else if (pathname.startsWith(prefix + '/')) {
|
|
148
|
+
pathname = pathname.substring(prefix.length) || '/'
|
|
149
|
+
} else if (this.config.verbose) {
|
|
150
|
+
Logger.warn(`Request out of application prefix: ${pathname} (expected: ${prefix}/*)`)
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
133
154
|
if (this.config.pathname && this.config.pathname !== '/') {
|
|
134
155
|
const appPathname = this.config.pathname.endsWith('/') ? this.config.pathname : `${this.config.pathname}/`
|
|
135
156
|
if (pathname.startsWith(appPathname)) {
|
package/source/core/builder.ts
CHANGED
|
@@ -117,6 +117,7 @@ export class RaitonBuilder implements BuilderInterface {
|
|
|
117
117
|
this.listenHmrSocket()
|
|
118
118
|
this.listenHmrMiddleware()
|
|
119
119
|
this.listenHmrHook()
|
|
120
|
+
this.listenHmrMcp()
|
|
120
121
|
this.watching();
|
|
121
122
|
}
|
|
122
123
|
|
|
@@ -178,6 +179,17 @@ export class RaitonBuilder implements BuilderInterface {
|
|
|
178
179
|
)
|
|
179
180
|
}
|
|
180
181
|
|
|
182
|
+
protected listenHmrMcp(): void {
|
|
183
|
+
Raiton.signals.listen(
|
|
184
|
+
'hmr:mcp',
|
|
185
|
+
async ({filename, version, timestamp}) => {
|
|
186
|
+
const imported = await import(`${filename}?v=${version || 1}&t=${timestamp || Date.now()}`)
|
|
187
|
+
Artifacts.reloadMcp(imported, filename)
|
|
188
|
+
Logger.log(LBadge.debug('HMR'), `[mcp] ${path.relative(this.workdir, filename)}`)
|
|
189
|
+
}
|
|
190
|
+
)
|
|
191
|
+
}
|
|
192
|
+
|
|
181
193
|
protected async build(thread: ThreadInterface): Promise<this> {
|
|
182
194
|
if (!this._source) throw new Error('Application source not found');
|
|
183
195
|
if (!this._out) throw new Error('Application output not found');
|
|
@@ -3,9 +3,10 @@ import {BuilderHMRDeclarationInterface} from "../../types";
|
|
|
3
3
|
import {LBadge, Logger} from "@protorians/logger";
|
|
4
4
|
import {compileController} from "./compiler";
|
|
5
5
|
import {compileSocket} from "../socket/builder";
|
|
6
|
+
import {compileMcp} from "../mcp/builder";
|
|
6
7
|
import {RaitonThread} from "../thread";
|
|
7
8
|
import {Injection} from "../injection";
|
|
8
|
-
import {isControllerArtifact, isSocketArtifact} from "../../framework";
|
|
9
|
+
import {isControllerArtifact, isSocketArtifact, isMcpArtifact} from "../../framework";
|
|
9
10
|
import path from "node:path";
|
|
10
11
|
import {ControllerRouteTracker} from "./tracker";
|
|
11
12
|
|
|
@@ -26,8 +27,9 @@ export class ControllerBuilder {
|
|
|
26
27
|
static async build<T>({filename, version, timestamp}: BuilderHMRDeclarationInterface): Promise<T | undefined> {
|
|
27
28
|
const isController = isControllerArtifact(filename);
|
|
28
29
|
const isSocket = isSocketArtifact(filename);
|
|
30
|
+
const isMcp = isMcpArtifact(filename);
|
|
29
31
|
|
|
30
|
-
if (!isController && !isSocket)
|
|
32
|
+
if (!isController && !isSocket && !isMcp)
|
|
31
33
|
return undefined;
|
|
32
34
|
|
|
33
35
|
const imported = await import(`${filename}?v=${version || 1}&t=${timestamp || Date.now()}`)
|
|
@@ -43,6 +45,10 @@ export class ControllerBuilder {
|
|
|
43
45
|
return compileSocket(artifact) as any;
|
|
44
46
|
}
|
|
45
47
|
|
|
48
|
+
if (isMcp) {
|
|
49
|
+
return compileMcp(artifact) as any;
|
|
50
|
+
}
|
|
51
|
+
|
|
46
52
|
const compilated = compileController(artifact, RaitonThread.current.application);
|
|
47
53
|
return compilated;
|
|
48
54
|
}
|
package/source/core/index.ts
CHANGED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import {registerMcpServer, unregisterMcpServer, getMcpServerMetadata} from "./metadata";
|
|
2
|
+
import {Injection} from "../injection";
|
|
3
|
+
import {McpServerRegistrationInterface} from "../../types";
|
|
4
|
+
import {McpRouteTracker} from "./tracker";
|
|
5
|
+
import {RaitonThread} from "../thread";
|
|
6
|
+
import {HttpMethod} from "../../framework/enums";
|
|
7
|
+
import {mcpHandleGet, mcpHandlePost, isMcpJsonRpc, SESSION_HEADER} from "../../framework/mcp/streamable-http";
|
|
8
|
+
import type {ContextInterface} from "../../types/core";
|
|
9
|
+
|
|
10
|
+
export function compileMcp(McpServerClass: any): McpServerRegistrationInterface | undefined {
|
|
11
|
+
const metadata = getMcpServerMetadata(McpServerClass.prototype || McpServerClass)
|
|
12
|
+
const name = metadata.name || McpServerClass.name
|
|
13
|
+
if (!metadata.path) return undefined
|
|
14
|
+
|
|
15
|
+
unregisterMcpServer(name)
|
|
16
|
+
Injection.invalidateCascade(name)
|
|
17
|
+
const entry = registerMcpServer(McpServerClass)
|
|
18
|
+
|
|
19
|
+
const app = RaitonThread.current?.application
|
|
20
|
+
const rootScope = (app as any)?.root
|
|
21
|
+
|
|
22
|
+
McpRouteTracker.removeRoutes(name, rootScope?.router)
|
|
23
|
+
|
|
24
|
+
if (app && rootScope?.router) {
|
|
25
|
+
const path = metadata.path
|
|
26
|
+
const postRoute = rootScope.route(
|
|
27
|
+
HttpMethod.POST,
|
|
28
|
+
path,
|
|
29
|
+
async (context: ContextInterface) => {
|
|
30
|
+
if (!isMcpJsonRpc(context.req.body)) {
|
|
31
|
+
return {error: {code: -32700, message: 'Invalid JSON-RPC request'}}
|
|
32
|
+
}
|
|
33
|
+
const existingSessionId = readSessionId(context)
|
|
34
|
+
const result = await mcpHandlePost(entry, context.req.body, existingSessionId)
|
|
35
|
+
context.reply.status(result.status)
|
|
36
|
+
if (result.headers[SESSION_HEADER]) {
|
|
37
|
+
context.reply.header(SESSION_HEADER, result.headers[SESSION_HEADER])
|
|
38
|
+
}
|
|
39
|
+
if (result.body === '') {
|
|
40
|
+
context.reply.send('')
|
|
41
|
+
return
|
|
42
|
+
}
|
|
43
|
+
return result.body
|
|
44
|
+
}
|
|
45
|
+
)
|
|
46
|
+
const getRoute = rootScope.route(
|
|
47
|
+
HttpMethod.GET,
|
|
48
|
+
path,
|
|
49
|
+
async (context: ContextInterface) => {
|
|
50
|
+
const existingSessionId = readSessionId(context)
|
|
51
|
+
const result = mcpHandleGet(entry, existingSessionId)
|
|
52
|
+
context.reply.status(result.status)
|
|
53
|
+
if (result.headers[SESSION_HEADER]) {
|
|
54
|
+
context.reply.header(SESSION_HEADER, result.headers[SESSION_HEADER])
|
|
55
|
+
}
|
|
56
|
+
return result.body
|
|
57
|
+
}
|
|
58
|
+
)
|
|
59
|
+
McpRouteTracker.setRoutes(name, [postRoute, getRoute])
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return entry
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function readSessionId(context: any): string | undefined {
|
|
66
|
+
const req = context.req
|
|
67
|
+
if (req?.headers?.get) {
|
|
68
|
+
const value = req.headers.get(SESSION_HEADER)
|
|
69
|
+
if (value) return value
|
|
70
|
+
}
|
|
71
|
+
if (req?.headers && typeof req.headers === 'object') {
|
|
72
|
+
const value = (req.headers as any)[SESSION_HEADER]
|
|
73
|
+
if (value) return value
|
|
74
|
+
}
|
|
75
|
+
return undefined
|
|
76
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import {McpServerMetaInterface, McpServerRegistrationInterface} from "../../types";
|
|
2
|
+
import {METADATA_KEYS} from "../../framework/constants";
|
|
3
|
+
import "reflect-metadata";
|
|
4
|
+
|
|
5
|
+
const registry = new Map<string, McpServerRegistrationInterface>();
|
|
6
|
+
|
|
7
|
+
export function getMcpServerMetadata(target: any): McpServerMetaInterface {
|
|
8
|
+
let metadata = Reflect.getMetadata(METADATA_KEYS.MCP, target);
|
|
9
|
+
if (!metadata) {
|
|
10
|
+
metadata = {
|
|
11
|
+
name: undefined,
|
|
12
|
+
version: '1.0.0',
|
|
13
|
+
description: undefined,
|
|
14
|
+
path: '/mcp',
|
|
15
|
+
instructions: undefined,
|
|
16
|
+
tools: [],
|
|
17
|
+
prompts: [],
|
|
18
|
+
resources: [],
|
|
19
|
+
resourceTemplates: [],
|
|
20
|
+
};
|
|
21
|
+
Reflect.defineMetadata(METADATA_KEYS.MCP, metadata, target);
|
|
22
|
+
}
|
|
23
|
+
return metadata;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function registerMcpServer(construct: any): McpServerRegistrationInterface {
|
|
27
|
+
const metadata: McpServerMetaInterface = getMcpServerMetadata(construct.prototype || construct);
|
|
28
|
+
const name = metadata.name || construct.name;
|
|
29
|
+
const path = normalizePath(metadata.path || '/mcp');
|
|
30
|
+
const entry: McpServerRegistrationInterface = {
|
|
31
|
+
path,
|
|
32
|
+
name,
|
|
33
|
+
version: metadata.version || '1.0.0',
|
|
34
|
+
description: metadata.description,
|
|
35
|
+
construct,
|
|
36
|
+
metadata,
|
|
37
|
+
};
|
|
38
|
+
registry.set(name, entry);
|
|
39
|
+
return entry;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function unregisterMcpServer(constructOrName: string | any): boolean {
|
|
43
|
+
if (typeof constructOrName === 'string') {
|
|
44
|
+
return registry.delete(constructOrName);
|
|
45
|
+
}
|
|
46
|
+
const metadata: McpServerMetaInterface = getMcpServerMetadata(constructOrName.prototype || constructOrName);
|
|
47
|
+
const name = metadata.name || constructOrName.name;
|
|
48
|
+
return registry.delete(name);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function getMcpServerRegistry(): Map<string, McpServerRegistrationInterface> {
|
|
52
|
+
return registry;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function getMcpServer(name: string): McpServerRegistrationInterface | undefined {
|
|
56
|
+
return registry.get(name);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function findMcpServerForPath(pathname: string, prefix?: string): McpServerRegistrationInterface | undefined {
|
|
60
|
+
const normalized = normalizePath(pathname);
|
|
61
|
+
for (const entry of registry.values()) {
|
|
62
|
+
if (entry.path === normalized) return entry;
|
|
63
|
+
if (prefix && prefix !== '/' && entry.path.startsWith(prefix)) {
|
|
64
|
+
if (entry.path === `${prefix}${normalized}`) return entry;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function clearMcpServerRegistry(): void {
|
|
71
|
+
registry.clear();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function normalizePath(pathname: string): string {
|
|
75
|
+
if (!pathname.startsWith('/')) pathname = `/${pathname}`;
|
|
76
|
+
if (pathname.length > 1 && pathname.endsWith('/')) {
|
|
77
|
+
pathname = pathname.slice(0, -1);
|
|
78
|
+
}
|
|
79
|
+
return pathname;
|
|
80
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import {Route} from "../router"
|
|
2
|
+
|
|
3
|
+
const mcpRouteMap = new Map<string, Route[]>()
|
|
4
|
+
|
|
5
|
+
export class McpRouteTracker {
|
|
6
|
+
static getRoutes(serverName: string): Route[] {
|
|
7
|
+
return mcpRouteMap.get(serverName) || []
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
static setRoutes(serverName: string, routes: Route[]): void {
|
|
11
|
+
mcpRouteMap.set(serverName, routes)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
static removeRoutes(serverName: string, router?: { remove(route: Route): void }): void {
|
|
15
|
+
const routes = mcpRouteMap.get(serverName)
|
|
16
|
+
if (!routes) return
|
|
17
|
+
if (router) {
|
|
18
|
+
for (const route of routes) {
|
|
19
|
+
router.remove(route)
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
mcpRouteMap.delete(serverName)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
static clear(): void {
|
|
26
|
+
mcpRouteMap.clear()
|
|
27
|
+
}
|
|
28
|
+
}
|
package/source/core/thread.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type {
|
|
|
2
2
|
BuilderInterface,
|
|
3
3
|
RuntimeAdapterInterface,
|
|
4
4
|
RuntimeServerInterface,
|
|
5
|
+
RuntimeServerHttpsInterface,
|
|
5
6
|
ThreadInterface,
|
|
6
7
|
ThreadOptionsInterface,
|
|
7
8
|
ThreadSetupOptionsInterface,
|
|
@@ -102,16 +103,31 @@ export class RaitonThread implements ThreadInterface {
|
|
|
102
103
|
const hostname = this.application.config.hostname || '0.0.0.0';
|
|
103
104
|
const displayHostname = (hostname === '0.0.0.0') ? (this.getNetworkIp() || 'localhost') : hostname;
|
|
104
105
|
const prefix = this.application.config.prefix
|
|
106
|
+
const httpsConfig = this.application.https
|
|
107
|
+
|
|
108
|
+
const httpsServerOptions: RuntimeServerHttpsInterface | undefined = httpsConfig?.enabled
|
|
109
|
+
? {
|
|
110
|
+
enabled: true,
|
|
111
|
+
environment: httpsConfig.environment,
|
|
112
|
+
certificate: httpsConfig.certificate!,
|
|
113
|
+
}
|
|
114
|
+
: undefined
|
|
105
115
|
|
|
106
116
|
this.runtimeServer = this.runtime.createServer(
|
|
107
117
|
this.application.handle.bind(this.application),
|
|
108
|
-
{prefix}
|
|
118
|
+
{prefix, https: httpsServerOptions}
|
|
109
119
|
)
|
|
110
120
|
|
|
121
|
+
const protocol = httpsConfig?.enabled ? 'https' : 'http'
|
|
122
|
+
|
|
111
123
|
await this.runtimeServer.listen(port, hostname)
|
|
112
124
|
|
|
113
|
-
Logger.log(LBadge.info('Local access:'),
|
|
114
|
-
Logger.log(LBadge.info('LAN access:'),
|
|
125
|
+
Logger.log(LBadge.info('Local access:'), `${protocol}://localhost:${port}${prefix ?? ''}`,)
|
|
126
|
+
Logger.log(LBadge.info('LAN access:'), `${protocol}://${displayHostname}:${port}${prefix ?? ''}`,)
|
|
127
|
+
|
|
128
|
+
if (httpsConfig?.enabled) {
|
|
129
|
+
Logger.log(LBadge.info('HTTPS:'), `enabled (${httpsConfig.environment ?? 'custom'})`,)
|
|
130
|
+
}
|
|
115
131
|
}
|
|
116
132
|
|
|
117
133
|
return this;
|
|
@@ -3,6 +3,7 @@ import {Logger} from "@protorians/logger";
|
|
|
3
3
|
import type {ConstructorType} from "../types";
|
|
4
4
|
import {Raiton} from "../core/raiton";
|
|
5
5
|
import {isArtifact} from "./utilities";
|
|
6
|
+
import {compileMcp} from "../core/mcp/builder";
|
|
6
7
|
|
|
7
8
|
export type HmrChannel =
|
|
8
9
|
| 'hmr:di'
|
|
@@ -10,6 +11,7 @@ export type HmrChannel =
|
|
|
10
11
|
| 'hmr:socket'
|
|
11
12
|
| 'hmr:middleware'
|
|
12
13
|
| 'hmr:hook'
|
|
14
|
+
| 'hmr:mcp'
|
|
13
15
|
|
|
14
16
|
export interface ArtifactClassification {
|
|
15
17
|
channel: HmrChannel
|
|
@@ -43,6 +45,7 @@ export const HMR_CHANNELS: Record<HmrChannel, readonly string[]> = {
|
|
|
43
45
|
'hmr:socket': ['socket'],
|
|
44
46
|
'hmr:middleware': ['middleware'],
|
|
45
47
|
'hmr:hook': ['hook', 'event', 'listener'],
|
|
48
|
+
'hmr:mcp': ['mcp'],
|
|
46
49
|
}
|
|
47
50
|
|
|
48
51
|
export class Artifacts {
|
|
@@ -77,6 +80,7 @@ export class Artifacts {
|
|
|
77
80
|
'model',
|
|
78
81
|
'schema',
|
|
79
82
|
'socket',
|
|
83
|
+
'mcp',
|
|
80
84
|
]
|
|
81
85
|
|
|
82
86
|
static register(type: string) {
|
|
@@ -204,4 +208,30 @@ export class Artifacts {
|
|
|
204
208
|
}
|
|
205
209
|
}
|
|
206
210
|
}
|
|
211
|
+
|
|
212
|
+
static reloadMcp(modulo: any, filename?: string) {
|
|
213
|
+
|
|
214
|
+
if (Raiton.thread?.builder.options.serve === false)
|
|
215
|
+
return Logger.warn(
|
|
216
|
+
'Artifact reload is only available in development mode'
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
const app = Raiton.thread?.application
|
|
220
|
+
if (!app) return
|
|
221
|
+
|
|
222
|
+
const rootScope = (app as any).root
|
|
223
|
+
if (!rootScope?.router) return
|
|
224
|
+
|
|
225
|
+
for (const mod of Object.values(modulo)) {
|
|
226
|
+
const name = (mod && typeof mod === 'object' && 'name' in mod) ? mod.name : (
|
|
227
|
+
typeof mod === 'function' ? mod.name ?? mod.constructor.name : undefined
|
|
228
|
+
);
|
|
229
|
+
|
|
230
|
+
if (typeof name === 'string' && typeof mod === 'function') {
|
|
231
|
+
Injection.invalidateCascade(name)
|
|
232
|
+
if (filename) Injection.registerArtifactPath(name, filename)
|
|
233
|
+
compileMcp(mod)
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
207
237
|
}
|
|
@@ -18,4 +18,8 @@ export const METADATA_KEYS = {
|
|
|
18
18
|
API_PROPERTY: Symbol('api:property'),
|
|
19
19
|
// Route interaction metadata key
|
|
20
20
|
ROUTE_INTERACTION: Symbol('route:interaction'),
|
|
21
|
+
// MCP metadata keys
|
|
22
|
+
MCP: Symbol('mcp:meta'),
|
|
23
|
+
MCP_ELEMENTS: Symbol('mcp:elements'),
|
|
24
|
+
MCP_ARGUMENTS: Symbol('mcp:arguments'),
|
|
21
25
|
}
|
|
@@ -7,4 +7,5 @@ export * from "./injection.decorator"
|
|
|
7
7
|
export * from "./guard.decorator"
|
|
8
8
|
export * from "./api-response.decorator"
|
|
9
9
|
export * from "./api.decorator"
|
|
10
|
-
export * from "./route-interaction.decorator"
|
|
10
|
+
export * from "./route-interaction.decorator"
|
|
11
|
+
export * from "./mcp.decorator"
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import {registerMcpServer, unregisterMcpServer, getMcpServerMetadata} from "../../core/mcp";
|
|
3
|
+
import {Injectable} from ".";
|
|
4
|
+
import {LifetimeEnum} from "@protorians/core";
|
|
5
|
+
import {METADATA_KEYS} from "../constants";
|
|
6
|
+
import type {
|
|
7
|
+
McpArgumentMetaInterface,
|
|
8
|
+
McpArgumentSchema,
|
|
9
|
+
McpElementMetaInterface,
|
|
10
|
+
McpElementType,
|
|
11
|
+
McpServerOptionsInterface,
|
|
12
|
+
} from "../../types";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Decorate a class as an MCP server. The class methods decorated with
|
|
16
|
+
* {@link McpTool}, {@link McpPrompt}, {@link McpResource} and
|
|
17
|
+
* {@link McpResourceTemplate} become the server's exposed elements.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* @McpServer({ name: 'math', version: '1.0.0', path: '/mcp' })
|
|
22
|
+
* class MathServer {
|
|
23
|
+
* @McpTool({ name: 'add', description: 'Add two numbers' })
|
|
24
|
+
* add(@McpArg('a', 'First operand', Number) a: number, @McpArg('b') b: number) {
|
|
25
|
+
* return a + b
|
|
26
|
+
* }
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export function McpServer(options: McpServerOptionsInterface = {}) {
|
|
31
|
+
return (target: any) => {
|
|
32
|
+
Injectable(LifetimeEnum.TRANSIENT, options.name || target.name)(target)
|
|
33
|
+
|
|
34
|
+
const meta = getMcpServerMetadata(target.prototype || target)
|
|
35
|
+
meta.name = options.name || target.name
|
|
36
|
+
meta.version = options.version || '1.0.0'
|
|
37
|
+
meta.description = options.description
|
|
38
|
+
meta.path = options.path || '/mcp'
|
|
39
|
+
meta.instructions = options.instructions
|
|
40
|
+
|
|
41
|
+
unregisterMcpServer(options.name || target.name)
|
|
42
|
+
registerMcpServer(target)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function createElementDecorator(type: McpElementType) {
|
|
47
|
+
return (nameOrOptions?: string | {name?: string; description?: string; uri?: string; mimeType?: string; title?: string}) =>
|
|
48
|
+
(target: any, propertyKey: string) => {
|
|
49
|
+
const meta = getMcpServerMetadata(target)
|
|
50
|
+
const name = typeof nameOrOptions === 'string'
|
|
51
|
+
? nameOrOptions
|
|
52
|
+
: nameOrOptions?.name || propertyKey
|
|
53
|
+
|
|
54
|
+
const element: McpElementMetaInterface = {
|
|
55
|
+
type,
|
|
56
|
+
name,
|
|
57
|
+
description: typeof nameOrOptions === 'string' ? undefined : nameOrOptions?.description,
|
|
58
|
+
propertyKey,
|
|
59
|
+
arguments: [],
|
|
60
|
+
uri: typeof nameOrOptions === 'object' ? nameOrOptions?.uri : undefined,
|
|
61
|
+
mimeType: typeof nameOrOptions === 'object' ? nameOrOptions?.mimeType : undefined,
|
|
62
|
+
title: typeof nameOrOptions === 'object' ? nameOrOptions?.title : undefined,
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const list = meta[elementChildren(type)]
|
|
66
|
+
if (list) list.push(element)
|
|
67
|
+
|
|
68
|
+
// Capture argument metadata supplied via parameter decorators
|
|
69
|
+
const arguments_ = Reflect.getMetadata(METADATA_KEYS.MCP_ARGUMENTS, target, propertyKey) || []
|
|
70
|
+
element.arguments = arguments_
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function elementChildren(type: McpElementType) {
|
|
75
|
+
return (type === 'tool' ? 'tools' : type === 'prompt' ? 'prompts' : type === 'resource' ? 'resources' : 'resourceTemplates')
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Expose a class method as an MCP tool.
|
|
80
|
+
*/
|
|
81
|
+
export function McpTool(options: {name?: string; description?: string} = {}) {
|
|
82
|
+
return createElementDecorator('tool')(options)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Expose a class method as an MCP prompt template.
|
|
87
|
+
*/
|
|
88
|
+
export function McpPrompt(options: {name?: string; description?: string} = {}) {
|
|
89
|
+
return createElementDecorator('prompt')(options)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Expose a class method as an MCP resource.
|
|
94
|
+
*/
|
|
95
|
+
export function McpResource(options: {uri: string; name?: string; description?: string; mimeType?: string}) {
|
|
96
|
+
return createElementDecorator('resource')({...options, name: options.name || options.uri})
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Expose a class method as an MCP resource template.
|
|
101
|
+
*/
|
|
102
|
+
export function McpResourceTemplate(options: {uriTemplate: string; name?: string; description?: string; mimeType?: string}) {
|
|
103
|
+
return createElementDecorator('resource-template')({
|
|
104
|
+
name: options.name || options.uriTemplate,
|
|
105
|
+
description: options.description,
|
|
106
|
+
uri: options.uriTemplate,
|
|
107
|
+
mimeType: options.mimeType,
|
|
108
|
+
})
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Shortcut to mark a method as a resource reader (deprecated form, kept for compatibility).
|
|
113
|
+
*/
|
|
114
|
+
export function McpRead(uri?: string) {
|
|
115
|
+
return createElementDecorator('resource')({uri: uri || ''})
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Declare an argument for an MCP tool, prompt or resource handler.
|
|
120
|
+
* Applied to method parameters, in declaration order.
|
|
121
|
+
*/
|
|
122
|
+
export function McpArg(
|
|
123
|
+
nameOrOptions: string | McpArgumentMetaInterface,
|
|
124
|
+
description?: string,
|
|
125
|
+
metatype?: any,
|
|
126
|
+
): any {
|
|
127
|
+
return (target: any, propertyKey: string, parameterIndex: number) => {
|
|
128
|
+
const arguments_ = Reflect.getMetadata(METADATA_KEYS.MCP_ARGUMENTS, target, propertyKey) || []
|
|
129
|
+
arguments_[parameterIndex] = normalizeArgument(nameOrOptions, description, metatype)
|
|
130
|
+
Reflect.defineMetadata(METADATA_KEYS.MCP_ARGUMENTS, arguments_, target, propertyKey)
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export {McpArg as McpArgument}
|
|
135
|
+
|
|
136
|
+
function normalizeArgument(
|
|
137
|
+
nameOrOptions: string | McpArgumentMetaInterface,
|
|
138
|
+
description?: string,
|
|
139
|
+
metatype?: any,
|
|
140
|
+
): McpArgumentMetaInterface {
|
|
141
|
+
if (typeof nameOrOptions === 'string') {
|
|
142
|
+
return {name: nameOrOptions, description, schema: schemaFromMetatype(metatype), metatype}
|
|
143
|
+
}
|
|
144
|
+
return {
|
|
145
|
+
name: nameOrOptions.name,
|
|
146
|
+
description: nameOrOptions.description,
|
|
147
|
+
schema: nameOrOptions.schema || schemaFromMetatype(nameOrOptions.metatype),
|
|
148
|
+
required: nameOrOptions.required,
|
|
149
|
+
metatype: nameOrOptions.metatype || metatype,
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function schemaFromMetatype(metatype?: any): McpArgumentSchema | undefined {
|
|
154
|
+
if (!metatype || metatype === Object) return undefined
|
|
155
|
+
const name = metatype.name
|
|
156
|
+
switch (name) {
|
|
157
|
+
case 'String':
|
|
158
|
+
return {type: 'string'}
|
|
159
|
+
case 'Number':
|
|
160
|
+
return {type: 'number'}
|
|
161
|
+
case 'Boolean':
|
|
162
|
+
return {type: 'boolean'}
|
|
163
|
+
case 'Object':
|
|
164
|
+
return {type: 'object'}
|
|
165
|
+
case 'Array':
|
|
166
|
+
return {type: 'array'}
|
|
167
|
+
default:
|
|
168
|
+
return undefined
|
|
169
|
+
}
|
|
170
|
+
}
|
|
@@ -5,6 +5,7 @@ export * from "./responses/index"
|
|
|
5
5
|
export * from "./decorators/index"
|
|
6
6
|
export * from "./plugins/index"
|
|
7
7
|
export * from "./runtime/index"
|
|
8
|
+
export * from "./mcp/index"
|
|
8
9
|
export * from "./exceptions/index"
|
|
9
10
|
export * from "./controllers"
|
|
10
11
|
export * from "./data-transfer-object"
|