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:
2026-04-08 11:45:52 +03:00
parent f0437d9390
commit 7f5a2bfe18
+16 -10
View File
@@ -11,8 +11,12 @@ import { BrowserService } from '../browser/browser.service';
import { CodeExecutorService } from '../code-executor/code-executor.service'; import { CodeExecutorService } from '../code-executor/code-executor.service';
import { ScenarioService } from '../scenario/scenario.service'; import { ScenarioService } from '../scenario/scenario.service';
import pkg from '../../package.json';
@Injectable() @Injectable()
export class McpService { export class McpService {
private readonly server: McpServer;
constructor( constructor(
private readonly authService: AuthService, private readonly authService: AuthService,
private readonly sessionService: SessionService, private readonly sessionService: SessionService,
@@ -20,9 +24,13 @@ export class McpService {
private readonly browserService: BrowserService, private readonly browserService: BrowserService,
private readonly codeExecutor: CodeExecutorService, private readonly codeExecutor: CodeExecutorService,
private readonly scenarioService: ScenarioService, 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 ────────────────────────────────────────────────────────────────── // ── Auth ──────────────────────────────────────────────────────────────────
@@ -446,14 +454,12 @@ export class McpService {
} }
async handle(req: Request, res: Response): Promise<void> { async handle(req: Request, res: Response): Promise<void> {
const server = new McpServer({ name: 'liquio-qa-bot', version: '1.0.0' }); const transport = new StreamableHTTPServerTransport({ sessionIdGenerator: undefined });
this.registerTools(server); await this.server.connect(transport);
try {
const transport = new StreamableHTTPServerTransport({
sessionIdGenerator: undefined, // stateless — no session management
});
await server.connect(transport);
await transport.handleRequest(req, res, req.body); await transport.handleRequest(req, res, req.body);
} finally {
await transport.close();
}
} }
} }