|
1 | 1 | import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; |
2 | | -import { State } from "./state.js"; |
| 2 | +import { Session } from "./session.js"; |
3 | 3 | import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js"; |
4 | | -import { registerAtlasTools } from "./tools/atlas/tools.js"; |
5 | | -import { registerMongoDBTools } from "./tools/mongodb/index.js"; |
| 4 | +import { AtlasTools } from "./tools/atlas/tools.js"; |
| 5 | +import { MongoDbTools } from "./tools/mongodb/index.js"; |
6 | 6 | import logger, { initializeLogger } from "./logger.js"; |
7 | 7 | import { mongoLogId } from "mongodb-log-writer"; |
8 | | -import { ApiClient } from "./common/atlas/apiClient.js"; |
9 | 8 |
|
10 | 9 | export class Server { |
11 | | - public readonly state: State; |
| 10 | + public readonly session: Session; |
12 | 11 | private readonly mcpServer: McpServer; |
| 12 | + private readonly transport: Transport; |
13 | 13 |
|
14 | | - constructor({ mcpServer, state, transport }: { mcpServer: McpServer; state: State; transport: Transport }) { |
15 | | - this.mcpServer = server; |
16 | | - this.state = new State(); |
| 14 | + constructor({ mcpServer, transport, session }: { mcpServer: McpServer; session: Session; transport: Transport }) { |
| 15 | + this.mcpServer = mcpServer; |
| 16 | + this.transport = transport; |
| 17 | + this.session = session; |
17 | 18 | } |
18 | 19 |
|
19 | | - async connect(transport: Transport) { |
20 | | - this.server = new McpServer({ |
21 | | - name: "MongoDB Atlas", |
22 | | - version: config.version, |
23 | | - }); |
| 20 | + async connect() { |
| 21 | + this.mcpServer.server.registerCapabilities({ logging: {} }); |
24 | 22 |
|
25 | | - this.server.server.registerCapabilities({ logging: {} }); |
| 23 | + this.registerTools(); |
26 | 24 |
|
27 | | - registerAtlasTools(this.server, this.state); |
28 | | - registerMongoDBTools(this.server, this.state); |
| 25 | + await initializeLogger(this.mcpServer); |
29 | 26 |
|
30 | | - await initializeLogger(this.server); |
31 | | - await this.server.connect(transport); |
| 27 | + await this.mcpServer.connect(this.transport); |
32 | 28 |
|
33 | | - logger.info(mongoLogId(1_000_004), "server", `Server started with transport ${transport.constructor.name}`); |
| 29 | + logger.info( |
| 30 | + mongoLogId(1_000_004), |
| 31 | + "server", |
| 32 | + `Server started with transport ${this.transport.constructor.name}` |
| 33 | + ); |
34 | 34 | } |
35 | 35 |
|
36 | 36 | async close(): Promise<void> { |
37 | 37 | try { |
38 | | - await this.state.serviceProvider?.close(true); |
| 38 | + await this.session.serviceProvider?.close(true); |
39 | 39 | } catch { |
40 | 40 | // Ignore errors during service provider close |
41 | 41 | } |
42 | | - await this.server?.close(); |
| 42 | + await this.mcpServer?.close(); |
| 43 | + } |
| 44 | + |
| 45 | + private registerTools() { |
| 46 | + for (const tool of [...AtlasTools, ...MongoDbTools]) { |
| 47 | + new tool(this.session).register(this.mcpServer); |
| 48 | + } |
43 | 49 | } |
44 | 50 | } |
0 commit comments