-
Notifications
You must be signed in to change notification settings - Fork 94
Add support for logging capability #521
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
0xKoller
wants to merge
12
commits into
canary
Choose a base branch
from
add-support-for-logging-capability-xmcp-409
base: canary
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
18af29c
logger
0xKoller eed09ba
Update logging.mdx
0xKoller 01e8d1a
log working
0xKoller 109d421
greptile fixes
0xKoller c6b8038
Update context.ts
0xKoller 1e2dcf5
Update context.ts
0xKoller da860cf
Merge branch 'canary' into add-support-for-logging-capability-xmcp-409
0xKoller be045ac
Merge branch 'canary' into add-support-for-logging-capability-xmcp-409
0xKoller 290d1c0
requested updates and concerns
0xKoller 2c2c05e
Merge branch 'canary' into add-support-for-logging-capability-xmcp-409
0xKoller 4527d75
Merge branch 'canary' into add-support-for-logging-capability-xmcp-409
0xKoller fe0853c
type safety and deployment behaviour
0xKoller 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| --- | ||
| title: "Logging" | ||
| metadataTitle: "Logging | xmcp Documentation" | ||
| publishedAt: "2026-03-17" | ||
| summary: "Send structured log messages from your MCP server to connected clients." | ||
| description: "The logging capability lets your tools, prompts, and resources send structured log messages to clients. Clients can control verbosity via log levels, and messages are automatically filtered based on the client's preference." | ||
| --- | ||
|
|
||
| xmcp supports the MCP [logging capability](https://modelcontextprotocol.io/specification/draft/server/utilities/logging), which provides a standardized way for servers to send structured log messages to clients. | ||
|
|
||
| ## Usage | ||
|
|
||
| Import `logger` from `xmcp` and call any log method directly in your handlers: | ||
|
|
||
| ```typescript title="src/tools/sync-data.ts" | ||
| import { z } from "zod"; | ||
| import { type InferSchema, logger } from "xmcp"; | ||
|
|
||
| export const schema = { | ||
| source: z.string().describe("Data source to sync from"), | ||
| }; | ||
|
|
||
| export const metadata = { | ||
| name: "sync-data", | ||
| description: "Sync data from an external source", | ||
| }; | ||
|
|
||
| export default async function syncData({ source }: InferSchema<typeof schema>) { | ||
| logger.info(`Starting sync from ${source}`, "sync-data"); | ||
|
|
||
| try { | ||
| const records = await fetchRecords(source); | ||
| logger.debug({ recordCount: records.length }, "sync-data"); | ||
|
|
||
| await processRecords(records); | ||
| logger.info("Sync completed successfully", "sync-data"); | ||
|
|
||
| return `Synced ${records.length} records`; | ||
| } catch (error) { | ||
| logger.error({ error: String(error), source }, "sync-data"); | ||
| return { isError: true, content: [{ type: "text", text: "Sync failed" }] }; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| No need to accept `extra` as a parameter, `logger` automatically resolves the current server session from async context. | ||
|
|
||
| ## Log Levels | ||
|
|
||
| The logging levels follow [RFC 5424](https://datatracker.ietf.org/doc/html/rfc5424#section-6.2.1) severity levels, from least to most severe: | ||
|
|
||
| | Method | Description | Example Use Case | | ||
| | ------------- | -------------------------------- | -------------------------- | | ||
| | `debug` | Detailed debugging information | Function entry/exit points | | ||
| | `info` | General informational messages | Operation progress updates | | ||
| | `notice` | Normal but significant events | Configuration changes | | ||
| | `warning` | Warning conditions | Deprecated feature usage | | ||
| | `error` | Error conditions | Operation failures | | ||
| | `critical` | Critical conditions | System component failures | | ||
| | `alert` | Action must be taken immediately | Data corruption detected | | ||
| | `emergency` | System is unusable | Complete system failure | | ||
|
|
||
| ## Sending a Message | ||
|
|
||
| Each log method has the same signature: | ||
|
|
||
| ```typescript | ||
| logger.info(data: unknown, loggerName?: string): void | ||
| ``` | ||
|
|
||
| - **`data`**: The log payload. Can be a string, number, object, or any JSON-serializable value. | ||
| - **`loggerName`** _(optional)_: A label to identify the source of the message (e.g., your tool name or a subsystem). | ||
|
|
||
| ## Client-Controlled Log Level | ||
|
|
||
| Clients can send a `logging/setLevel` request to control which messages they receive. When a client sets the level to `"warning"`, only messages at `warning` severity and above are delivered; `debug`, `info`, and `notice` messages are silently dropped. | ||
|
|
||
| If no level has been set by the client, all messages are sent. | ||
|
|
||
| ## Works Everywhere | ||
|
|
||
| Logging is available in all handler types: | ||
|
|
||
| ```typescript title="src/prompts/analyze.ts" | ||
| import { logger } from "xmcp"; | ||
|
|
||
| export default async function analyze(args: any) { | ||
| logger.info("Running analysis prompt", "analyze"); | ||
| return "Analysis result..."; | ||
| } | ||
| ``` | ||
|
|
||
| ```typescript title="src/resources/status.ts" | ||
| import { logger } from "xmcp"; | ||
|
|
||
| export default async function status(args: any) { | ||
| logger.debug("Fetching system status", "status"); | ||
| return "System OK"; | ||
| } | ||
| ``` |
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 |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
| "tools", | ||
| "prompts", | ||
| "resources", | ||
| "logging", | ||
| "middlewares", | ||
| "css", | ||
| "external-clients" | ||
|
|
||
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,3 @@ | ||
| .vercel | ||
| .xmcp | ||
| xmcp-env.d.ts |
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,16 @@ | ||
| { | ||
| "name": "Logging", | ||
| "description": "Learn how to send structured log messages to clients using the MCP logging capability", | ||
| "keywords": [ | ||
| "logging" | ||
| ], | ||
| "scripts": { | ||
| "build": "xmcp build", | ||
| "dev": "xmcp dev", | ||
| "start": "node dist/http.js" | ||
| }, | ||
| "dependencies": { | ||
| "xmcp": "workspace:*", | ||
| "zod": "^4.0.10" | ||
| } | ||
| } |
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,22 @@ | ||
| import { z } from "zod"; | ||
| import { type InferSchema, type ToolMetadata, logger } from "xmcp"; | ||
|
|
||
| export const schema = { | ||
| a: z.number().describe("First number"), | ||
| b: z.number().describe("Second number"), | ||
| }; | ||
|
|
||
| export const metadata: ToolMetadata = { | ||
| name: "add", | ||
| description: "Add two numbers together", | ||
| }; | ||
|
|
||
| export default async function add({ a, b }: InferSchema<typeof schema>) { | ||
| logger.info(`Adding ${a} + ${b}`, "add"); | ||
|
|
||
| const result = a + b; | ||
|
|
||
| logger.debug({ a, b, result }, "add"); | ||
|
|
||
| return `Result: ${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,11 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "target": "es2017", | ||
| "module": "commonjs", | ||
| "esModuleInterop": true, | ||
| "forceConsistentCasingInFileNames": true, | ||
| "strict": true, | ||
| "skipLibCheck": true | ||
| }, | ||
| "include": ["xmcp-env.d.ts", "src/**/*.ts"] | ||
| } |
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,14 @@ | ||
| import { XmcpConfig } from "xmcp"; | ||
|
|
||
| const config: XmcpConfig = { | ||
| http: true, | ||
| paths: { | ||
| prompts: false, | ||
| resources: false, | ||
| }, | ||
| typescript: { | ||
| skipTypeCheck: true, | ||
| }, | ||
| }; | ||
|
|
||
| export default config; |
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.
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.