Skip to content

Commit 662854b

Browse files
committed
fix: mcp context proto
1 parent 22807f7 commit 662854b

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

plugin/controller/lib/impl/mcp/MCPControllerRegister.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,21 @@ export class MCPControllerRegister implements ControllerRegister {
344344
this.app.logger.error('session %s error %o', transport.sessionId, error);
345345
};
346346
const messageFunc = transport.onmessage;
347+
const self = this;
347348
transport.onmessage = async (...args: [ JSONRPCMessage ]) => {
348-
await ctx.app.ctxStorage.run(ctx, async () => {
349-
await mw(ctx, async () => {
349+
// 这里需要 new 一个新的 ctx,否则 ContextProto 会未被初始化
350+
const socket = new Socket();
351+
const req = new IncomingMessage(socket);
352+
const res = new ServerResponse(req);
353+
req.method = 'POST';
354+
req.url = self.mcpConfig.sseInitPath;
355+
req.headers = {
356+
accept: 'application/json, text/event-stream',
357+
'content-type': 'application/json',
358+
};
359+
const newCtx = self.app.createContext(req, res) as unknown as Context;
360+
await ctx.app.ctxStorage.run(newCtx, async () => {
361+
await mw(newCtx, async () => {
350362
await messageFunc!(...args);
351363
});
352364
});

plugin/controller/test/fixtures/apps/mcp-app/app/controller/McpController.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
PromptArgsSchema,
1212
Logger,
1313
Inject,
14+
ContextProto,
1415
ToolArgsSchema,
1516
} from '@eggjs/tegg';
1617
import z from 'zod';
@@ -25,13 +26,26 @@ export const ToolType = {
2526
}),
2627
};
2728

29+
@ContextProto()
30+
export class CommonService {
31+
@Inject()
32+
logger: Logger;
33+
34+
async sayHello(): Promise<string> {
35+
this.logger.info('hello world');
36+
return 'hello world';
37+
}
38+
}
2839

2940
@MCPController()
3041
export class McpController {
3142

3243
@Inject()
3344
logger: Logger;
3445

46+
@Inject()
47+
commonService: CommonService;
48+
3549
@MCPPrompt()
3650
async foo(@PromptArgsSchema(PromptType) args: PromptArgs<typeof PromptType>): Promise<MCPPromptResponse> {
3751
this.logger.info('hello world');
@@ -50,6 +64,7 @@ export class McpController {
5064

5165
@MCPTool()
5266
async bar(@ToolArgsSchema(ToolType) args: ToolArgs<typeof ToolType>): Promise<MCPToolResponse> {
67+
await this.commonService.sayHello();
5368
return {
5469
content: [
5570
{

plugin/controller/test/mcp/mcp.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ describe('plugin/controller/test/mcp/mcp.test.ts', () => {
9393
return app.close();
9494
});
9595

96-
it('sse should work', async () => {
96+
it.only('sse should work', async () => {
9797
const sseClient = new Client({
9898
name: 'sse-demo-client',
9999
version: '1.0.0',

0 commit comments

Comments
 (0)