refactor(mcp): hoist McpServer to singleton, read name/version from package.json
- server instantiated once in constructor, tools registered once - per-request transport created fresh and closed in finally to reset server state - name and version sourced from package.json instead of hard-coded strings
This commit is contained in:
+17
-11
@@ -11,8 +11,12 @@ import { BrowserService } from '../browser/browser.service';
|
||||
import { CodeExecutorService } from '../code-executor/code-executor.service';
|
||||
import { ScenarioService } from '../scenario/scenario.service';
|
||||
|
||||
import pkg from '../../package.json';
|
||||
|
||||
@Injectable()
|
||||
export class McpService {
|
||||
private readonly server: McpServer;
|
||||
|
||||
constructor(
|
||||
private readonly authService: AuthService,
|
||||
private readonly sessionService: SessionService,
|
||||
@@ -20,9 +24,13 @@ export class McpService {
|
||||
private readonly browserService: BrowserService,
|
||||
private readonly codeExecutor: CodeExecutorService,
|
||||
private readonly scenarioService: ScenarioService,
|
||||
) {}
|
||||
) {
|
||||
this.server = new McpServer({ name: pkg.name, version: pkg.version });
|
||||
this.registerTools();
|
||||
}
|
||||
|
||||
private registerTools(server: McpServer): void {
|
||||
private registerTools(): void {
|
||||
const server = this.server;
|
||||
|
||||
// ── Auth ──────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -446,14 +454,12 @@ export class McpService {
|
||||
}
|
||||
|
||||
async handle(req: Request, res: Response): Promise<void> {
|
||||
const server = new McpServer({ name: 'liquio-qa-bot', version: '1.0.0' });
|
||||
this.registerTools(server);
|
||||
|
||||
const transport = new StreamableHTTPServerTransport({
|
||||
sessionIdGenerator: undefined, // stateless — no session management
|
||||
});
|
||||
|
||||
await server.connect(transport);
|
||||
await transport.handleRequest(req, res, req.body);
|
||||
const transport = new StreamableHTTPServerTransport({ sessionIdGenerator: undefined });
|
||||
await this.server.connect(transport);
|
||||
try {
|
||||
await transport.handleRequest(req, res, req.body);
|
||||
} finally {
|
||||
await transport.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user