-
Notifications
You must be signed in to change notification settings - Fork 41
refactor: rename state to session, combine tool registration, and clearer dependencies #55
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
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
2febc5d
refactor: remove type assertions and simplify state setup
gagik 6c8752b
fix: tests and token refactor
gagik cfc8d89
refactor: re-introduce the server class
gagik 0f03288
chore: add type-powered eslint rules
gagik 0ee6f01
Merge branch 'gagik/strict-eslint' of github.com:mongodb-js/mongodb-m…
gagik 7e0a287
fix: adapt the tst
gagik 97aa690
Merge branch 'main' of github.com:mongodb-js/mongodb-mcp-server into …
gagik 8062288
wip
gagik 2b596fe
Merge branch 'main' of github.com:mongodb-js/mongodb-mcp-server into …
gagik c202de5
Merge branch 'gagik/strict-eslint' of github.com:mongodb-js/mongodb-m…
gagik 8e4f19c
refactor: adapt to latest structure
gagik 2a87387
refactor: adapt to the latest structure
gagik 3c50e08
Merge branch 'main' of github.com:mongodb-js/mongodb-mcp-server into …
gagik e434bae
fix: style
gagik 4d9e651
refactor: make the session close
gagik 94ffeee
Update src/tools/atlas/atlasTool.ts
gagik 0bfd2aa
refactor: align tools naming
gagik 4c96ad9
add changes
gagik 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 |
---|---|---|
@@ -1,19 +1,30 @@ | ||
#!/usr/bin/env node | ||
|
||
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; | ||
import { Server } from "./server.js"; | ||
import logger from "./logger.js"; | ||
import { mongoLogId } from "mongodb-log-writer"; | ||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; | ||
import config from "./config.js"; | ||
import { Session } from "./session.js"; | ||
import { Server } from "./server.js"; | ||
|
||
export async function runServer() { | ||
const server = new Server(); | ||
try { | ||
const session = new Session(); | ||
const mcpServer = new McpServer({ | ||
name: "MongoDB Atlas", | ||
version: config.version, | ||
}); | ||
|
||
const server = new Server({ | ||
mcpServer, | ||
session, | ||
}); | ||
|
||
const transport = new StdioServerTransport(); | ||
await server.connect(transport); | ||
} | ||
|
||
runServer().catch((error) => { | ||
logger.emergency(mongoLogId(1_000_004), "server", `Fatal error running server: ${error}`); | ||
await server.connect(transport); | ||
} catch (error: unknown) { | ||
logger.emergency(mongoLogId(1_000_004), "server", `Fatal error running server: ${error as string}`); | ||
|
||
process.exit(1); | ||
}); | ||
} |
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 |
---|---|---|
@@ -1,39 +1,40 @@ | ||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; | ||
import defaultState, { State } from "./state.js"; | ||
import { Session } from "./session.js"; | ||
import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js"; | ||
import { registerAtlasTools } from "./tools/atlas/tools.js"; | ||
import { registerMongoDBTools } from "./tools/mongodb/index.js"; | ||
import config from "./config.js"; | ||
import { AtlasTools } from "./tools/atlas/tools.js"; | ||
import { MongoDbTools } from "./tools/mongodb/tools.js"; | ||
import logger, { initializeLogger } from "./logger.js"; | ||
import { mongoLogId } from "mongodb-log-writer"; | ||
|
||
export class Server { | ||
state: State = defaultState; | ||
private server?: McpServer; | ||
public readonly session: Session; | ||
private readonly mcpServer: McpServer; | ||
|
||
constructor({ mcpServer, session }: { mcpServer: McpServer; session: Session }) { | ||
this.mcpServer = mcpServer; | ||
this.session = session; | ||
} | ||
|
||
async connect(transport: Transport) { | ||
this.server = new McpServer({ | ||
name: "MongoDB Atlas", | ||
version: config.version, | ||
}); | ||
this.mcpServer.server.registerCapabilities({ logging: {} }); | ||
|
||
this.server.server.registerCapabilities({ logging: {} }); | ||
this.registerTools(); | ||
|
||
registerAtlasTools(this.server, this.state); | ||
registerMongoDBTools(this.server, this.state); | ||
await initializeLogger(this.mcpServer); | ||
|
||
await initializeLogger(this.server); | ||
await this.server.connect(transport); | ||
await this.mcpServer.connect(transport); | ||
|
||
logger.info(mongoLogId(1_000_004), "server", `Server started with transport ${transport.constructor.name}`); | ||
} | ||
|
||
async close(): Promise<void> { | ||
try { | ||
await this.state.serviceProvider?.close(true); | ||
} catch { | ||
// Ignore errors during service provider close | ||
await this.session.close(); | ||
await this.mcpServer.close(); | ||
} | ||
|
||
private registerTools() { | ||
for (const tool of [...AtlasTools, ...MongoDbTools]) { | ||
new tool(this.session).register(this.mcpServer); | ||
} | ||
await this.server?.close(); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import { ToolBase } from "../tool.js"; | ||
import { State } from "../../state.js"; | ||
import { Session } from "../../session.js"; | ||
|
||
export abstract class AtlasToolBase extends ToolBase { | ||
constructor(state: State) { | ||
super(state); | ||
constructor(protected readonly session: Session) { | ||
super(session); | ||
} | ||
} |
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯