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.
- package/CHANGELOG.md +6 -0
- package/build/bin/index.mjs +20009 -1769
- package/docs/user-guide/README.md +6 -4
- package/docs/user-guide/decorators/README.md +2 -0
- package/docs/user-guide/decorators/mcp.md +70 -0
- package/docs/user-guide/mcp.md +120 -0
- package/docs/user-guide/plugins.md +1 -0
- package/package.json +1 -1
- 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/framework/artifacts.ts +29 -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/utilities/artifact.util.ts +4 -0
- package/source/framework/utilities/openapi.utils.ts +81 -0
- package/source/types/index.ts +2 -1
- package/source/types/mcp.ts +118 -0
- package/source/types/raiton.ts +1 -0
|
@@ -22,6 +22,7 @@ Ce dossier décrit comment utiliser Raiton pour créer un serveur d'API.
|
|
|
22
22
|
| [https.md](https.md) | HTTPS, certificats et environnements |
|
|
23
23
|
| [openapi/README.md](openapi/README.md) | Générer et documenter l'API |
|
|
24
24
|
| [sockets.md](sockets.md) | Déclarer et gérer les sockets |
|
|
25
|
+
| [mcp.md](mcp.md) | Exposer outils, prompts et ressources via le protocole MCP |
|
|
25
26
|
| [tutorial.md](tutorial.md) | Construire une mini API complète de bout en bout |
|
|
26
27
|
| [plugins.md](plugins.md) | Vue d'ensemble des plugins intégrés |
|
|
27
28
|
| [types.md](types.md) | Comprendre les types utiles au développeur |
|
|
@@ -46,10 +47,11 @@ Ce dossier décrit comment utiliser Raiton pour créer un serveur d'API.
|
|
|
46
47
|
14. lire [https.md](https.md)
|
|
47
48
|
15. lire [openapi/README.md](openapi/README.md)
|
|
48
49
|
16. lire [sockets.md](sockets.md)
|
|
49
|
-
17. lire [
|
|
50
|
-
18. lire [
|
|
51
|
-
19. lire [
|
|
52
|
-
20. lire [
|
|
50
|
+
17. lire [mcp.md](mcp.md)
|
|
51
|
+
18. lire [types.md](types.md)
|
|
52
|
+
19. lire [tutorial.md](tutorial.md)
|
|
53
|
+
20. lire [plugins.md](plugins.md)
|
|
54
|
+
21. lire [cli.md](cli.md)
|
|
53
55
|
|
|
54
56
|
## Ce que couvre ce guide
|
|
55
57
|
|
|
@@ -21,11 +21,13 @@ Cette section regroupe les décorateurs que vous utilisez pour définir vos cont
|
|
|
21
21
|
- [api-query.md](api-query.md)
|
|
22
22
|
- [route-interaction.md](route-interaction.md)
|
|
23
23
|
- [guard.md](guard.md)
|
|
24
|
+
- [mcp.md](mcp.md)
|
|
24
25
|
|
|
25
26
|
## Comment lire cette section
|
|
26
27
|
|
|
27
28
|
- commencez par `controllable.md` et `routes.md`
|
|
28
29
|
- utilisez ensuite `parameters.md`, `injection.md` et `middleware.md`
|
|
30
|
+
- documentez vos endpoints temps réel et IA avec `socket.md` et `mcp.md`
|
|
29
31
|
- terminez par les décorateurs OpenAPI si vous documentez votre API
|
|
30
32
|
|
|
31
33
|
---
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# `@McpServer()` et décorateurs MCP
|
|
2
|
+
|
|
3
|
+
> **Navigation :** [← guard](guard.md) | [README →](README.md)
|
|
4
|
+
|
|
5
|
+
Les décorateurs MCP servent à définir un serveur **Model Context Protocol** : outils, prompts et ressources accessibles aux clients IA.
|
|
6
|
+
|
|
7
|
+
## Décorateurs concernés
|
|
8
|
+
|
|
9
|
+
- `@McpServer()`
|
|
10
|
+
- `@McpTool()`
|
|
11
|
+
- `@McpPrompt()`
|
|
12
|
+
- `@McpResource()`
|
|
13
|
+
- `@McpResourceTemplate()`
|
|
14
|
+
- `@McpArg()` (alias `@McpArgument()`)
|
|
15
|
+
|
|
16
|
+
## Pourquoi utilisé
|
|
17
|
+
|
|
18
|
+
- regrouper une logique exposée aux clients IA dans une classe
|
|
19
|
+
- garder une structure cohérente avec les contrôleurs HTTP
|
|
20
|
+
- réutiliser l’injection de dépendances
|
|
21
|
+
|
|
22
|
+
## Comment l’utiliser
|
|
23
|
+
|
|
24
|
+
- marquez la classe avec `@McpServer({ name, version, path })`
|
|
25
|
+
- ajoutez les éléments sur les méthodes
|
|
26
|
+
- déclarez les paramètres avec `@McpArg` dans l’ordre des arguments
|
|
27
|
+
- si une méthode n’a aucun `@McpArg`, elle reçoit l’objet d’entrée complet en premier argument
|
|
28
|
+
|
|
29
|
+
## Exemple
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
import { McpServer, McpTool, McpResource, McpArg } from "raiton/framework"
|
|
33
|
+
|
|
34
|
+
@McpServer({ name: "math", version: "1.0.0", path: "/mcp" })
|
|
35
|
+
export class MathServer {
|
|
36
|
+
@McpTool({ name: "add", description: "Additionne deux nombres" })
|
|
37
|
+
add(
|
|
38
|
+
@McpArg("a", "Premier opérande", Number) a: number,
|
|
39
|
+
@McpArg("b", "Second opérande", Number) b: number,
|
|
40
|
+
) {
|
|
41
|
+
return a + b
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
@McpResource({ uri: "config://app", name: "Configuration", mimeType: "application/json" })
|
|
45
|
+
config() {
|
|
46
|
+
return { env: "production" }
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Options
|
|
52
|
+
|
|
53
|
+
`@McpServer` accepte `name`, `version`, `description`, `path` et `instructions`.
|
|
54
|
+
|
|
55
|
+
`@McpResourceTemplate` accepte `uriTemplate`, `name`, `description` et `mimeType` ; les variables `{...}` de l’URI sont transmises à la méthode.
|
|
56
|
+
|
|
57
|
+
## Avantages
|
|
58
|
+
|
|
59
|
+
- API déclarative et lisible
|
|
60
|
+
- métadonnées de type pour générer automatiquement le schéma des arguments
|
|
61
|
+
- pas de serveur séparé : cohabite avec HTTP et sockets
|
|
62
|
+
|
|
63
|
+
## Inconvénients
|
|
64
|
+
|
|
65
|
+
- l’inférence de type repose sur le métatype fourni à `@McpArg`
|
|
66
|
+
- certains éléments (ex. notifications SSE) restent simples
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
[← guard](guard.md) | [README →](README.md)
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# MCP (Model Context Protocol)
|
|
2
|
+
|
|
3
|
+
> **Navigation :** [← sockets](sockets.md) | [types →](types.md)
|
|
4
|
+
|
|
5
|
+
Raiton permet de créer un serveur **MCP (Model Context Protocol)** facilement, avec des décorateurs, et de le faire coexister avec le serveur HTTP et les sockets sur le même processus et le même port.
|
|
6
|
+
|
|
7
|
+
## De quoi s’agit-il
|
|
8
|
+
|
|
9
|
+
MCP est un protocole qui expose des **outils (tools)**, des **prompts** et des **ressources (resources)** à des clients IA (assistants, agents, IDE…). Raiton implémente le transport **Streamable HTTP** : un simple endpoint qui accepte des requêtes JSON-RPC `POST`, identique au reste de l’API.
|
|
10
|
+
|
|
11
|
+
## Pourquoi utilisé
|
|
12
|
+
|
|
13
|
+
- exposer une logique applicative existante à des assistants IA
|
|
14
|
+
- réutiliser les services et l’injection de dépendances existants
|
|
15
|
+
- cohabiter avec les contrôleurs HTTP et les sockets sans serveur séparé
|
|
16
|
+
- bénéficier du HMR, des artifacts et d’OpenAPI, comme pour le reste du framework
|
|
17
|
+
|
|
18
|
+
## Comment l’utiliser
|
|
19
|
+
|
|
20
|
+
1. Créez un fichier artifact `*.mcp.ts` (reconnu automatiquement)
|
|
21
|
+
2. Déclarez une classe avec `@McpServer()`
|
|
22
|
+
3. Ajoutez des méthodes avec `@McpTool()`, `@McpPrompt()`, `@McpResource()` ou `@McpResourceTemplate()`
|
|
23
|
+
4. Déclarez les paramètres avec `@McpArg()`
|
|
24
|
+
5. Le chemin `path` du serveur devient automatiquement accessible en `POST`
|
|
25
|
+
|
|
26
|
+
## Exemple
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { McpServer, McpTool, McpPrompt, McpResource, McpResourceTemplate, McpArg } from "raiton/framework"
|
|
30
|
+
|
|
31
|
+
@McpServer({ name: "math", version: "1.0.0", path: "/mcp" })
|
|
32
|
+
export class MathServer {
|
|
33
|
+
@McpTool({ name: "add", description: "Additionne deux nombres" })
|
|
34
|
+
add(
|
|
35
|
+
@McpArg("a", "Premier opérande", Number) a: number,
|
|
36
|
+
@McpArg("b", "Second opérande", Number) b: number,
|
|
37
|
+
) {
|
|
38
|
+
return a + b
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
@McpPrompt({ name: "resume", description: "Résume un texte" })
|
|
42
|
+
resume(@McpArg("text", "Le texte à résumer", String) text: string) {
|
|
43
|
+
return `Résume le texte suivant :\n\n${text}`
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
@McpResource({ uri: "config://app", name: "Configuration", mimeType: "application/json" })
|
|
47
|
+
config() {
|
|
48
|
+
return { env: "production" }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
@McpResourceTemplate({ uriTemplate: "user://{id}", name: "Utilisateur par id" })
|
|
52
|
+
user({ id }: any) {
|
|
53
|
+
return { id, name: `User-${id}` }
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Aucun enregistrement supplémentaire n’est nécessaire : le scanner détecte `*.mcp.ts`, compile la classe, enregistre le serveur dans le registre MCP et ajoute automatiquement les routes `POST /mcp` et `GET /mcp` sur l’application.
|
|
59
|
+
|
|
60
|
+
## Points d’extensibilité
|
|
61
|
+
|
|
62
|
+
### Enregistrement manuel
|
|
63
|
+
|
|
64
|
+
Vous pouvez aussi compiler et enregistrer un serveur MCP explicitement :
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
import { compileMcp } from "raiton/core"
|
|
68
|
+
|
|
69
|
+
compileMcp(MathServer)
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Plugin MCP
|
|
73
|
+
|
|
74
|
+
Le plugin `mcpPlugin()` permet un montage explicite sur un chemin personnalisé et d’activer le endpoint `GET` :
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
import { app } from "raiton"
|
|
78
|
+
|
|
79
|
+
app.register(mcpPlugin({ path: "/mcp", enableGet: true }))
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
> Les routes étant normalement auto-générées par la compilation, le plugin est surtout utile lorsque vous gérez vous-même l’enregistrement ou souhaitez un contrôle fin.
|
|
83
|
+
|
|
84
|
+
## Protocole
|
|
85
|
+
|
|
86
|
+
Le endpoint répond au protocole MCP sur JSON-RPC 2.0 :
|
|
87
|
+
|
|
88
|
+
- `initialize` — négociation de la version et des capacités
|
|
89
|
+
- `ping` — test de disponibilité
|
|
90
|
+
- `tools/list` / `tools/call`
|
|
91
|
+
- `prompts/list` / `prompts/get`
|
|
92
|
+
- `resources/list` / `resources/templates/list` / `resources/read`
|
|
93
|
+
- `logging/setLevel` / `notifications/initialized`
|
|
94
|
+
|
|
95
|
+
La version du protocole est vérifiée : une version non supportée renvoie `406`. Les réponses utilisent le header `mcp-session-id`.
|
|
96
|
+
|
|
97
|
+
## HMR
|
|
98
|
+
|
|
99
|
+
Les fichiers `*.mcp.ts` bénéficient du **hot reload** : une modification recharge le serveur MCP sans redémarrer le process, exactement comme les contrôleurs et les sockets.
|
|
100
|
+
|
|
101
|
+
## OpenAPI
|
|
102
|
+
|
|
103
|
+
Les serveurs MCP sont documentés dans le contrat OpenAPI généré, avec une extension `x-mcp` listant outils, prompts et ressources.
|
|
104
|
+
|
|
105
|
+
## Avantages
|
|
106
|
+
|
|
107
|
+
- intégration native avec le serveur HTTP existant
|
|
108
|
+
- réutilise l’injection de dépendances
|
|
109
|
+
- HMR, artifacts et OpenAPI homogènes avec le reste du framework
|
|
110
|
+
- aucun serveur ni processus séparé
|
|
111
|
+
|
|
112
|
+
## Inconvénients
|
|
113
|
+
|
|
114
|
+
- transport limité au Streamable HTTP (pas de stdio fourni)
|
|
115
|
+
- les notifications serveur `GET` (SSE) restent basiques
|
|
116
|
+
- le protocole évolue vite, la liste des versions supportées doit être suivie
|
|
117
|
+
|
|
118
|
+
---
|
|
119
|
+
|
|
120
|
+
[← sockets](sockets.md) | [types →](types.md)
|
|
@@ -9,6 +9,7 @@ Les plugins servent à brancher des fonctionnalités réutilisables à l’éche
|
|
|
9
9
|
- [body-parser.md](body-parser.md)
|
|
10
10
|
- [security.md](security.md)
|
|
11
11
|
- [openapi/README.md](openapi/README.md)
|
|
12
|
+
- [mcp.md](mcp.md) (plugin optionnel `mcpPlugin()`)
|
|
12
13
|
|
|
13
14
|
## Vue d’ensemble
|
|
14
15
|
|
package/package.json
CHANGED
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
|
+
}
|
|
@@ -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 {
|
|
@@ -205,4 +208,30 @@ export class Artifacts {
|
|
|
205
208
|
}
|
|
206
209
|
}
|
|
207
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
|
+
}
|
|
208
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"
|