-
Notifications
You must be signed in to change notification settings - Fork 44
feat: add mcp #307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: add mcp #307
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { | ||
| EggProtoImplClass, | ||
| } from '@eggjs/tegg-types'; | ||
| import { MCPInfoUtil } from '../../../src/util/MCPInfoUtil'; | ||
|
|
||
| export function Extra() { | ||
| return function( | ||
| target: any, | ||
| propertyKey: PropertyKey, | ||
| parameterIndex: number, | ||
| ) { | ||
| const controllerClazz = target.constructor as EggProtoImplClass; | ||
| const methodName = propertyKey as string; | ||
| MCPInfoUtil.setMCPExtra(parameterIndex, controllerClazz, methodName); | ||
| }; | ||
| } |
33 changes: 33 additions & 0 deletions
33
core/controller-decorator/src/decorator/mcp/MCPController.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { ControllerType, MCPControllerParams, AccessLevel, EggProtoImplClass } from '@eggjs/tegg-types'; | ||
| import { PrototypeUtil, SingletonProto } from '@eggjs/core-decorator'; | ||
| import ControllerInfoUtil from '../../util/ControllerInfoUtil'; | ||
| import { StackUtil } from '@eggjs/tegg-common-util'; | ||
| import { MCPInfoUtil } from '../../../src/util/MCPInfoUtil'; | ||
|
|
||
| export function MCPController(param?: MCPControllerParams) { | ||
| return function(constructor: EggProtoImplClass) { | ||
| const func = SingletonProto({ | ||
| accessLevel: AccessLevel.PUBLIC, | ||
| name: param?.protoName, | ||
| }); | ||
| func(constructor); | ||
| ControllerInfoUtil.setControllerType(constructor, ControllerType.MCP); | ||
| if (param?.controllerName) { | ||
| ControllerInfoUtil.setControllerName(constructor, param?.controllerName); | ||
| } | ||
| // './tegg/core/common-util/src/StackUtil.ts', | ||
| // './tegg/core/core-decorator/src/decorator/Prototype.ts', | ||
| // './tegg/core/controller-decorator/src/decorator/tr/TRController.ts', | ||
| // './tegg/core/core-decorator/node_modules/_reflect-metadata@0.1.13@reflect-metadata/Reflect.js', | ||
| // './tegg/core/core-decorator/node_modules/_reflect-metadata@0.1.13@reflect-metadata/Reflect.js', | ||
| // './tegg/core/controller-decorator/test/fixtures/TRFooController.ts', | ||
| PrototypeUtil.setFilePath(constructor, StackUtil.getCalleeFromStack(false, 5)); | ||
|
|
||
| if (param?.name) { | ||
| MCPInfoUtil.setMCPName(param.name, constructor); | ||
| } | ||
| if (param?.version) { | ||
| MCPInfoUtil.setMCPVersion(param.version, constructor); | ||
| } | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { | ||
| ControllerType, | ||
| EggProtoImplClass, | ||
| MCPPromptParams, | ||
| } from '@eggjs/tegg-types'; | ||
| import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; | ||
| import { MCPInfoUtil } from '../../../src/util/MCPInfoUtil'; | ||
| import MethodInfoUtil from '../../../src/util/MethodInfoUtil'; | ||
|
|
||
| export function MCPPrompt(params?: MCPPromptParams) { | ||
| return function( | ||
| target: any, | ||
| propertyKey: PropertyKey, | ||
| ) { | ||
| const controllerClazz = target.constructor as EggProtoImplClass; | ||
| const methodName = propertyKey as string; | ||
| MethodInfoUtil.setMethodControllerType( | ||
| controllerClazz, | ||
| methodName, | ||
| ControllerType.MCP, | ||
| ); | ||
| MCPInfoUtil.setMCPPromptParams( | ||
| { | ||
| ...params, | ||
| mcpName: params?.name, | ||
| name: methodName, | ||
| }, | ||
| controllerClazz, | ||
| methodName, | ||
| ); | ||
| MCPInfoUtil.setMCPPrompt(controllerClazz, methodName); | ||
| }; | ||
| } | ||
|
|
||
| export function PromptArgsSchema(argsSchema: Parameters<McpServer['prompt']>['2']) { | ||
| return function( | ||
| target: any, | ||
| propertyKey: PropertyKey, | ||
| parameterIndex: number, | ||
| ) { | ||
| const controllerClazz = target.constructor as EggProtoImplClass; | ||
| const methodName = propertyKey as string; | ||
| MCPInfoUtil.setMCPPromptArgsInArgs( | ||
| { | ||
| argsSchema, | ||
| index: parameterIndex, | ||
| }, | ||
| controllerClazz, | ||
| methodName, | ||
| ); | ||
| }; | ||
| } |
32 changes: 32 additions & 0 deletions
32
core/controller-decorator/src/decorator/mcp/MCPResource.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { | ||
| ControllerType, | ||
| EggProtoImplClass, | ||
| MCPResourceParams, | ||
| } from '@eggjs/tegg-types'; | ||
| import { MCPInfoUtil } from '../../../src/util/MCPInfoUtil'; | ||
| import MethodInfoUtil from '../../../src/util/MethodInfoUtil'; | ||
|
|
||
| export function MCPResource(params: MCPResourceParams) { | ||
| return function( | ||
| target: any, | ||
| propertyKey: PropertyKey, | ||
| ) { | ||
| const controllerClazz = target.constructor as EggProtoImplClass; | ||
| const methodName = propertyKey as string; | ||
| MethodInfoUtil.setMethodControllerType( | ||
| controllerClazz, | ||
| methodName, | ||
| ControllerType.MCP, | ||
| ); | ||
| MCPInfoUtil.setMCPResourceParams( | ||
| { | ||
| ...params, | ||
| mcpName: params.name, | ||
| name: methodName, | ||
| }, | ||
| controllerClazz, | ||
| methodName, | ||
| ); | ||
| MCPInfoUtil.setMCPResource(controllerClazz, methodName); | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { | ||
| ControllerType, | ||
| EggProtoImplClass, | ||
| MCPToolParams, | ||
| } from '@eggjs/tegg-types'; | ||
| import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; | ||
| import { MCPInfoUtil } from '../../../src/util/MCPInfoUtil'; | ||
| import MethodInfoUtil from '../../../src/util/MethodInfoUtil'; | ||
|
|
||
| export function MCPTool(params?: MCPToolParams) { | ||
| return function( | ||
| target: any, | ||
| propertyKey: PropertyKey, | ||
| ) { | ||
| const controllerClazz = target.constructor as EggProtoImplClass; | ||
| const methodName = propertyKey as string; | ||
| MethodInfoUtil.setMethodControllerType( | ||
| controllerClazz, | ||
| methodName, | ||
| ControllerType.MCP, | ||
| ); | ||
| MCPInfoUtil.setMCPToolParams( | ||
| { | ||
| ...params, | ||
| mcpName: params?.name, | ||
| name: methodName, | ||
| }, | ||
| controllerClazz, | ||
| methodName, | ||
| ); | ||
| MCPInfoUtil.setMCPTool(controllerClazz, methodName); | ||
| }; | ||
| } | ||
|
|
||
| export function ToolArgsSchema(argsSchema: Parameters<McpServer['tool']>['2']) { | ||
| return function( | ||
| target: any, | ||
| propertyKey: PropertyKey, | ||
| parameterIndex: number, | ||
| ) { | ||
| const controllerClazz = target.constructor as EggProtoImplClass; | ||
| const methodName = propertyKey as string; | ||
| MCPInfoUtil.setMCPToolArgsInArgs( | ||
| { | ||
| argsSchema, | ||
| index: parameterIndex, | ||
| }, | ||
| controllerClazz, | ||
| methodName, | ||
| ); | ||
| }; | ||
| } |
87 changes: 87 additions & 0 deletions
87
core/controller-decorator/src/impl/mcp/MCPControllerMetaBuilder.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import assert from 'assert'; | ||
| import { PrototypeUtil } from '@eggjs/core-decorator'; | ||
| import { MCPPromptMeta, MCPResourceMeta, MCPToolMeta } from '../../model'; | ||
| import { ControllerValidator } from '../../util/validator/ControllerValidator'; | ||
| import ControllerInfoUtil from '../../util/ControllerInfoUtil'; | ||
| import { MCPControllerPromptMetaBuilder } from './MCPControllerPromptMetaBuilder'; | ||
| import { MCPControllerResourceMetaBuilder } from './MCPControllerResourceMetaBuilder'; | ||
| import { MCPControllerToolMetaBuilder } from './MCPControllerToolMetaBuilder'; | ||
| import { MCPInfoUtil } from '../../../src/util/MCPInfoUtil'; | ||
| import { ControllerType, EggProtoImplClass } from '@eggjs/tegg-types'; | ||
| import { MCPControllerMeta } from '../../../src/model/MCPControllerMeta'; | ||
| import { ControllerMetaBuilderFactory } from '../../builder/ControllerMetaBuilderFactory'; | ||
|
|
||
| export class MCPControllerMetaBuilder { | ||
| private readonly clazz: EggProtoImplClass; | ||
|
|
||
| constructor(clazz: EggProtoImplClass) { | ||
| this.clazz = clazz; | ||
| } | ||
|
|
||
| private buildResource(): MCPResourceMeta[] { | ||
| const methodNames = MCPInfoUtil.getMCPResource(this.clazz); | ||
| const methods: MCPResourceMeta[] = []; | ||
| for (const methodName of methodNames) { | ||
| const builder = new MCPControllerResourceMetaBuilder(this.clazz, methodName); | ||
| const methodMeta = builder.build(); | ||
| if (methodMeta) { | ||
| methods.push(methodMeta); | ||
| } | ||
| } | ||
| return methods; | ||
| } | ||
|
|
||
| private buildPrompt(): MCPPromptMeta[] { | ||
| const methodNames = MCPInfoUtil.getMCPPrompt(this.clazz); | ||
| const methods: MCPPromptMeta[] = []; | ||
| for (const methodName of methodNames) { | ||
| const builder = new MCPControllerPromptMetaBuilder(this.clazz, methodName); | ||
| const methodMeta = builder.build(); | ||
| if (methodMeta) { | ||
| methods.push(methodMeta); | ||
| } | ||
| } | ||
| return methods; | ||
| } | ||
|
|
||
| private buildTool(): MCPToolMeta[] { | ||
| const methodNames = MCPInfoUtil.getMCPTool(this.clazz); | ||
| const methods: MCPToolMeta[] = []; | ||
| for (const methodName of methodNames) { | ||
| const builder = new MCPControllerToolMetaBuilder(this.clazz, methodName); | ||
| const methodMeta = builder.build(); | ||
| if (methodMeta) { | ||
| methods.push(methodMeta); | ||
| } | ||
| } | ||
| return methods; | ||
| } | ||
|
|
||
| build(): MCPControllerMeta { | ||
| ControllerValidator.validate(this.clazz); | ||
| const controllerType = ControllerInfoUtil.getControllerType(this.clazz); | ||
| assert(controllerType === ControllerType.MCP, 'invalidate controller type'); | ||
| const mcpMiddlewares = ControllerInfoUtil.getControllerMiddlewares(this.clazz); | ||
| const resources = this.buildResource(); | ||
| const prompts = this.buildPrompt(); | ||
| const tools = this.buildTool(); | ||
| const property = PrototypeUtil.getProperty(this.clazz); | ||
| const protoName = property!.name as string; | ||
| const clazzName = this.clazz.name; | ||
| const controllerName = ControllerInfoUtil.getControllerName(this.clazz) || clazzName; | ||
| const name = MCPInfoUtil.getMCPName(this.clazz) || controllerName; | ||
| const version = MCPInfoUtil.getMCPVersion(this.clazz) || '1.0.0'; | ||
| const needAcl = ControllerInfoUtil.hasControllerAcl(this.clazz); | ||
| const aclCode = ControllerInfoUtil.getControllerAcl(this.clazz); | ||
| return new MCPControllerMeta( | ||
| clazzName, protoName, controllerName, name, version, tools, resources, | ||
| prompts, mcpMiddlewares, needAcl, aclCode, | ||
| ); | ||
| } | ||
|
|
||
| static create(clazz: EggProtoImplClass) { | ||
| return new MCPControllerMetaBuilder(clazz); | ||
| } | ||
| } | ||
|
|
||
| ControllerMetaBuilderFactory.registerControllerMetaBuilder(ControllerType.MCP, MCPControllerMetaBuilder.create); |
39 changes: 39 additions & 0 deletions
39
core/controller-decorator/src/impl/mcp/MCPControllerPromptMetaBuilder.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import type { EggProtoImplClass } from '@eggjs/tegg-types'; | ||
| import { MCPPromptMeta } from '../../model'; | ||
| import { MethodValidator } from '../../util/validator/MethodValidator'; | ||
| import MethodInfoUtil from '../../util/MethodInfoUtil'; | ||
| import { MCPInfoUtil } from '../../util/MCPInfoUtil'; | ||
|
|
||
| export class MCPControllerPromptMetaBuilder { | ||
| private readonly clazz: EggProtoImplClass; | ||
| private readonly methodName: string; | ||
|
|
||
| constructor(clazz: EggProtoImplClass, methodName: string) { | ||
| this.clazz = clazz; | ||
| this.methodName = methodName; | ||
| } | ||
|
|
||
| build(): MCPPromptMeta | undefined { | ||
| MethodValidator.validate(this.clazz, this.methodName); | ||
| const controllerType = MethodInfoUtil.getMethodControllerType(this.clazz, this.methodName); | ||
| if (!controllerType) { | ||
| return undefined; | ||
| } | ||
akitaSummer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const middlewares = MethodInfoUtil.getMethodMiddlewares(this.clazz, this.methodName); | ||
| const needAcl = MethodInfoUtil.hasMethodAcl(this.clazz, this.methodName); | ||
| const aclCode = MethodInfoUtil.getMethodAcl(this.clazz, this.methodName); | ||
| const params = MCPInfoUtil.getMCPPromptParams(this.clazz, this.methodName); | ||
| const detail = MCPInfoUtil.getMCPPromptArgsIndex(this.clazz, this.methodName); | ||
| const extra = MCPInfoUtil.getMCPExtra(this.clazz, this.methodName); | ||
|
|
||
| return new MCPPromptMeta({ | ||
| name: this.methodName, | ||
| middlewares, | ||
| needAcl, | ||
| aclCode, | ||
| detail, | ||
| extra, | ||
| ...params, | ||
| }); | ||
| } | ||
| } | ||
37 changes: 37 additions & 0 deletions
37
core/controller-decorator/src/impl/mcp/MCPControllerResourceMetaBuilder.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import type { EggProtoImplClass } from '@eggjs/tegg-types'; | ||
| import { MCPResourceMeta } from '../../model'; | ||
| import { MethodValidator } from '../../util/validator/MethodValidator'; | ||
| import MethodInfoUtil from '../../util/MethodInfoUtil'; | ||
| import { MCPInfoUtil } from '../../util/MCPInfoUtil'; | ||
|
|
||
| export class MCPControllerResourceMetaBuilder { | ||
| private readonly clazz: EggProtoImplClass; | ||
| private readonly methodName: string; | ||
|
|
||
| constructor(clazz: EggProtoImplClass, methodName: string) { | ||
| this.clazz = clazz; | ||
| this.methodName = methodName; | ||
| } | ||
|
|
||
| build(): MCPResourceMeta | undefined { | ||
| MethodValidator.validate(this.clazz, this.methodName); | ||
| const controllerType = MethodInfoUtil.getMethodControllerType(this.clazz, this.methodName); | ||
| if (!controllerType) { | ||
| return undefined; | ||
| } | ||
| const middlewares = MethodInfoUtil.getMethodMiddlewares(this.clazz, this.methodName); | ||
| const needAcl = MethodInfoUtil.hasMethodAcl(this.clazz, this.methodName); | ||
| const aclCode = MethodInfoUtil.getMethodAcl(this.clazz, this.methodName); | ||
| const params = MCPInfoUtil.getMCPResourceParams(this.clazz, this.methodName); | ||
| const extra = MCPInfoUtil.getMCPExtra(this.clazz, this.methodName); | ||
|
|
||
| return new MCPResourceMeta({ | ||
| name: this.methodName, | ||
| middlewares, | ||
| needAcl, | ||
| aclCode, | ||
| extra, | ||
| ...params, | ||
| }); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.