Skip to content

initalize logging after connection #611

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cli/src/client/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ export async function connect(
): Promise<void> {
try {
await client.connect(transport);

if (client.getServerCapabilities()?.logging) {
// default logging level is undefined in the spec, but the user of the
// inspector most likely wants debug.
await client.setLoggingLevel("debug");
}
} catch (error) {
throw new Error(
`Failed to connect to MCP server: ${error instanceof Error ? error.message : String(error)}`,
Expand Down
1 change: 1 addition & 0 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ const App = () => {
]);
},
getRoots: () => rootsRef.current,
defaultLoggingLevel: logLevel,
});

useEffect(() => {
Expand Down
7 changes: 7 additions & 0 deletions client/src/lib/hooks/useConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
ToolListChangedNotificationSchema,
PromptListChangedNotificationSchema,
Progress,
LoggingLevel,
} from "@modelcontextprotocol/sdk/types.js";
import { RequestOptions } from "@modelcontextprotocol/sdk/shared/protocol.js";
import { useState } from "react";
Expand Down Expand Up @@ -63,6 +64,7 @@ interface UseConnectionOptions {
onPendingRequest?: (request: any, resolve: any, reject: any) => void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
getRoots?: () => any[];
defaultLoggingLevel?: LoggingLevel;
}

export function useConnection({
Expand All @@ -78,6 +80,7 @@ export function useConnection({
onStdErrNotification,
onPendingRequest,
getRoots,
defaultLoggingLevel,
}: UseConnectionOptions) {
const [connectionStatus, setConnectionStatus] =
useState<ConnectionStatus>("disconnected");
Expand Down Expand Up @@ -521,6 +524,10 @@ export function useConnection({
});
}

if (capabilities?.logging && defaultLoggingLevel) {
await client.setLoggingLevel(defaultLoggingLevel);
}

setMcpClient(client);
setConnectionStatus("connected");
} catch (e) {
Expand Down