-
Notifications
You must be signed in to change notification settings - Fork 166
chore: improve descriptions about connection - MCP-224 #592
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
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
676ca27
chore: validate apiBaseUrl
blva acf048c
address comment
blva 67923d2
chore: improve descriptions about connection - MCP-224
blva 91bffa8
fix test
blva c95b9cb
Merge remote-tracking branch 'origin/main' into MCP-224
blva d53e39f
address copilot comments
blva 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,12 +62,15 @@ export class Server { | |
| } | ||
|
|
||
| async connect(transport: Transport): Promise<void> { | ||
| // Resources are now reactive, so we register them ASAP so they can listen to events like | ||
| await this.validateConfig(); | ||
| // Register resources after the server is initialized so they can listen to events like | ||
| // connection events. | ||
| this.registerResources(); | ||
| await this.validateConfig(); | ||
|
|
||
| this.mcpServer.server.registerCapabilities({ logging: {}, resources: { listChanged: true, subscribe: true } }); | ||
| this.mcpServer.server.registerCapabilities({ | ||
| logging: {}, | ||
| resources: { listChanged: true, subscribe: true }, | ||
| instructions: this.getInstructions(), | ||
| }); | ||
|
|
||
| // TODO: Eventually we might want to make tools reactive too instead of relying on custom logic. | ||
| this.registerTools(); | ||
|
|
@@ -134,17 +137,17 @@ export class Server { | |
| message: `Server with version ${packageInfo.version} started with transport ${transport.constructor.name} and agent runner ${JSON.stringify(this.session.mcpClient)}`, | ||
| }); | ||
|
|
||
| this.emitServerEvent("start", Date.now() - this.startTime); | ||
| this.emitServerTelemetryEvent("start", Date.now() - this.startTime); | ||
| }; | ||
|
|
||
| this.mcpServer.server.onclose = (): void => { | ||
| const closeTime = Date.now(); | ||
| this.emitServerEvent("stop", Date.now() - closeTime); | ||
| this.emitServerTelemetryEvent("stop", Date.now() - closeTime); | ||
| }; | ||
|
|
||
| this.mcpServer.server.onerror = (error: Error): void => { | ||
| const closeTime = Date.now(); | ||
| this.emitServerEvent("stop", Date.now() - closeTime, error); | ||
| this.emitServerTelemetryEvent("stop", Date.now() - closeTime, error); | ||
| }; | ||
|
|
||
| await this.mcpServer.connect(transport); | ||
|
|
@@ -161,17 +164,18 @@ export class Server { | |
| } | ||
|
|
||
| public sendResourceUpdated(uri: string): void { | ||
| this.session.logger.info({ | ||
| id: LogId.serverInitialized, | ||
| context: "resources", | ||
| message: `Resource updated: ${uri}`, | ||
| }); | ||
|
|
||
| if (this.subscriptions.has(uri)) { | ||
| void this.mcpServer.server.sendResourceUpdated({ uri }); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Emits a server event | ||
| * @param command - The server command (e.g., "start", "stop", "register", "deregister") | ||
| * @param additionalProperties - Additional properties specific to the event | ||
| */ | ||
| private emitServerEvent(command: ServerCommand, commandDuration: number, error?: Error): void { | ||
| private emitServerTelemetryEvent(command: ServerCommand, commandDuration: number, error?: Error): void { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. flyby: emitServerEvent feels misleading, so I renamed it |
||
| const event: ServerEvent = { | ||
| timestamp: new Date().toISOString(), | ||
| source: "mdbmcp", | ||
|
|
@@ -239,6 +243,13 @@ export class Server { | |
| // Validate API client credentials | ||
| if (this.userConfig.apiClientId && this.userConfig.apiClientSecret) { | ||
| try { | ||
| if (!this.userConfig.apiBaseUrl.startsWith("https://")) { | ||
| const message = | ||
| "Failed to validate MongoDB Atlas the credentials from config: apiBaseUrl must start with https://"; | ||
| console.error(message); | ||
| throw new Error(message); | ||
| } | ||
|
|
||
| await this.session.apiClient.validateAccessToken(); | ||
| } catch (error) { | ||
| if (this.userConfig.connectionString === undefined) { | ||
|
|
@@ -255,6 +266,24 @@ export class Server { | |
| } | ||
| } | ||
|
|
||
| private getInstructions(): string { | ||
| let instructions = ` | ||
| This is the MongoDB MCP server. | ||
| `; | ||
| if (this.userConfig.connectionString) { | ||
| instructions = ` | ||
| This MCP server was configured with a MongoDB connection string, and you can assume that you are connected to a MongoDB cluster. | ||
| `; | ||
| } | ||
|
|
||
| if (this.userConfig.apiClientId && this.userConfig.apiClientSecret) { | ||
| instructions = ` | ||
blva marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| This MCP server was configured with MongoDB Atlas API credentials.`; | ||
| } | ||
|
|
||
| return instructions; | ||
| } | ||
|
|
||
| private async connectToConfigConnectionString(): Promise<void> { | ||
| if (this.userConfig.connectionString) { | ||
| try { | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.