Skip to content

Commit

Permalink
feat: allow to set server logging level
Browse files Browse the repository at this point in the history
  • Loading branch information
punkpeye committed Dec 27, 2024
1 parent affea71 commit 513746a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/FastMCP.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const runWithTestServer = async ({
start,
}: {
start: () => Promise<FastMCP>;
run: ({ client }: { client: Client }) => Promise<void>;
run: ({ client, server }: { client: Client, server: FastMCP }) => Promise<void>;
}) => {
const port = await getRandomPort();

Expand Down Expand Up @@ -47,7 +47,7 @@ const runWithTestServer = async ({

await client.connect(transport);

await run({ client });
await run({ client, server });
} finally {
await server.stop();
}
Expand Down Expand Up @@ -261,6 +261,32 @@ test("tracks tool progress", async () => {
});
});

test("sets logging levels", async () => {
await runWithTestServer({
start: async () => {
const server = new FastMCP({
name: "Test",
version: "1.0.0",
});

return server;
},
run: async ({ client, server }) => {
await client.setLoggingLevel("debug");

expect(
server.loggingLevel,
).toBe("debug");

await client.setLoggingLevel("info");

expect(
server.loggingLevel,
).toBe("info");
},
});
});

test("adds resources", async () => {
await runWithTestServer({
start: async () => {
Expand Down
14 changes: 14 additions & 0 deletions src/FastMCP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
McpError,
ReadResourceRequestSchema,
ServerCapabilities,
SetLevelRequestSchema,
} from "@modelcontextprotocol/sdk/types.js";
import { zodToJsonSchema } from "zod-to-json-schema";
import type { z } from "zod";
Expand Down Expand Up @@ -107,6 +108,7 @@ export class FastMCP {
#prompts: Prompt[];
#server: Server | null = null;
#options: ServerOptions;
#loggingLevel: LoggingLevel = "info";

constructor(public options: ServerOptions) {
this.#options = options;
Expand All @@ -129,6 +131,12 @@ export class FastMCP {
if (this.#prompts.length) {
this.setupPromptHandlers(server);
}

server.setRequestHandler(SetLevelRequestSchema, (request) => {
this.#loggingLevel = request.params.level;

return {};
});
}

private setupErrorHandling(server: Server) {
Expand All @@ -141,6 +149,10 @@ export class FastMCP {
});
}

public get loggingLevel() {
return this.#loggingLevel;
}

private setupToolHandlers(server: Server) {
server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
Expand Down Expand Up @@ -380,6 +392,8 @@ export class FastMCP {
capabilities.prompts = {};
}

capabilities.logging = {};

this.#server = new Server(
{ name: this.#options.name, version: this.#options.version },
{ capabilities },
Expand Down

0 comments on commit 513746a

Please sign in to comment.