-
Notifications
You must be signed in to change notification settings - Fork 96
chore: Reactive resource support for debugging connectivity MCP-80 #413
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 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
84aa8ba
chore: Probe of concept for reactive resources based on the session
kmruiz ce6d2e9
chore: Add debug resource relevant events
kmruiz 26e1407
chore: Fix compilation error
kmruiz c042cff
chore: Fix more compilation and linting issues
kmruiz 76ddf15
chore: force pinging the database, as the provider does not test it
kmruiz c5b696b
chore: Fix linting issues
kmruiz 235ffcf
chore: PR improvements and use notify one single change
kmruiz 7dc6746
chore: Change name and uri of the debug resource
kmruiz 550a284
chore: it seems that resourceListChanged is mandatory
kmruiz e50d706
chore: Gracefully fail when the server is down
kmruiz d0790b5
chore: Rename to connect for consistency
kmruiz 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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { ReactiveResource } from "../resource.js"; | ||
import { config } from "../../common/config.js"; | ||
import type { UserConfig } from "../../common/config.js"; | ||
|
||
export class ConfigResource extends ReactiveResource( | ||
{ | ||
name: "config", | ||
uri: "config://config", | ||
config: { | ||
description: | ||
"Server configuration, supplied by the user either as environment variables or as startup arguments", | ||
}, | ||
}, | ||
{ | ||
initial: { ...config }, | ||
events: [], | ||
} | ||
) { | ||
reduce(previous: UserConfig, eventName: undefined, event: undefined): UserConfig { | ||
kmruiz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
void event; | ||
return previous; | ||
} | ||
|
||
toOutput(state: UserConfig): string { | ||
const result = { | ||
telemetry: state.telemetry, | ||
logPath: state.logPath, | ||
connectionString: state.connectionString | ||
? "set; access to MongoDB tools are currently available to use" | ||
: "not set; before using any MongoDB tool, you need to configure a connection string, alternatively you can setup MongoDB Atlas access, more info at 'https://github.com/mongodb-js/mongodb-mcp-server'.", | ||
connectOptions: state.connectOptions, | ||
atlas: | ||
state.apiClientId && state.apiClientSecret | ||
? "set; MongoDB Atlas tools are currently available to use" | ||
: "not set; MongoDB Atlas tools are currently unavailable, to have access to MongoDB Atlas tools like creating clusters or connecting to clusters make sure to setup credentials, more info at 'https://github.com/mongodb-js/mongodb-mcp-server'.", | ||
}; | ||
|
||
return JSON.stringify(result); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { ReactiveResource } from "../resource.js"; | ||
import { config } from "../../common/config.js"; | ||
import type { UserConfig } from "../../common/config.js"; | ||
|
||
type ConnectionStateDebuggingInformation = { | ||
readonly tag: "connected" | "connecting" | "disconnected" | "errored"; | ||
readonly connectionStringAuthType?: "scram" | "ldap" | "kerberos" | "oidc-auth-flow" | "oidc-device-flow" | "x.509"; | ||
readonly oidcLoginUrl?: string; | ||
readonly oidcUserCode?: string; | ||
readonly errorReason?: string; | ||
}; | ||
|
||
export class DebugResource extends ReactiveResource( | ||
{ | ||
name: "debug", | ||
uri: "config://debug", | ||
kmruiz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
config: { | ||
description: "Debugging information for connectivity issues.", | ||
}, | ||
}, | ||
{ | ||
initial: { tag: "disconnected" }, | ||
events: ["connected", "disconnect", "close"], | ||
} | ||
) { | ||
reduce( | ||
previous: ConnectionStateDebuggingInformation, | ||
eventName: "connected" | "disconnect" | "close", | ||
event: undefined | ||
kmruiz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
): ConnectionStateDebuggingInformation { | ||
void event; | ||
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. Is this necessary anymore, now that you're using |
||
|
||
switch (eventName) { | ||
case "connected": | ||
return { tag: "connected" }; | ||
case "disconnect": | ||
return { tag: "disconnected" }; | ||
case "close": | ||
return { tag: "disconnected" }; | ||
} | ||
} | ||
|
||
toOutput(state: ConnectionStateDebuggingInformation): string { | ||
const result = { | ||
connectionStatus: state.tag, | ||
}; | ||
|
||
return JSON.stringify(result); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { Server } from "../server.js"; | ||
import { Session } from "../common/session.js"; | ||
import { UserConfig } from "../common/config.js"; | ||
import { Telemetry } from "../telemetry/telemetry.js"; | ||
import type { SessionEvents } from "../common/session.js"; | ||
import { ReadResourceCallback, RegisteredResource, ResourceMetadata } from "@modelcontextprotocol/sdk/server/mcp.js"; | ||
|
||
type PayloadOf<K extends keyof SessionEvents> = SessionEvents[K][0]; | ||
|
||
type ResourceConfiguration = { name: string; uri: string; config: ResourceMetadata }; | ||
|
||
export function ReactiveResource<V, KE extends readonly (keyof SessionEvents)[]>( | ||
kmruiz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ name, uri, config: resourceConfig }: ResourceConfiguration, | ||
{ | ||
initial, | ||
events, | ||
}: { | ||
initial: V; | ||
events: KE; | ||
} | ||
) { | ||
type E = KE[number]; | ||
|
||
abstract class NewReactiveResource { | ||
private registeredResource?: RegisteredResource; | ||
|
||
constructor( | ||
protected readonly server: Server, | ||
protected readonly session: Session, | ||
protected readonly config: UserConfig, | ||
protected readonly telemetry: Telemetry, | ||
private current?: V | ||
) { | ||
this.current = initial; | ||
|
||
for (const event of events) { | ||
this.session.on(event, (...args: SessionEvents[typeof event]) => { | ||
this.current = this.reduce(this.current, event, (args as unknown[])[0] as PayloadOf<typeof event>); | ||
this.triggerUpdate(); | ||
}); | ||
} | ||
} | ||
|
||
public register(): void { | ||
this.registeredResource = this.server.mcpServer.registerResource( | ||
name, | ||
uri, | ||
resourceConfig, | ||
this.resourceCallback | ||
); | ||
} | ||
|
||
private resourceCallback: ReadResourceCallback = (uri) => ({ | ||
contents: [ | ||
{ | ||
text: this.toOutput(this.current), | ||
mimeType: "application/json", | ||
uri: uri.href, | ||
}, | ||
], | ||
}); | ||
|
||
private triggerUpdate() { | ||
this.registeredResource?.update({}); | ||
this.server.mcpServer.sendResourceListChanged(); | ||
kmruiz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
abstract reduce(previous: V | undefined, eventName: E, ...event: PayloadOf<E>[]): V; | ||
abstract toOutput(state: V | undefined): string; | ||
} | ||
|
||
return NewReactiveResource; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { ConfigResource } from "./common/config.js"; | ||
import { DebugResource } from "./common/debug.js"; | ||
|
||
export const Resources = [ConfigResource, DebugResource] as const; |
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.