Skip to content

Commit e642a56

Browse files
committed
feat: spec-based proxy server logging
1 parent 5c71b26 commit e642a56

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

src/lib/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { EventEmitter } from 'events'
2+
import { LoggingLevel } from '@modelcontextprotocol/sdk/types.js'
23

34
/**
45
* Options for creating an OAuth client provider
@@ -33,3 +34,12 @@ export interface OAuthCallbackServerOptions {
3334
/** Event emitter to signal when auth code is received */
3435
events: EventEmitter
3536
}
37+
38+
/*
39+
* Message sending helper type
40+
*/
41+
export interface MCPLogMessageParams {
42+
level: LoggingLevel
43+
logger: string
44+
data: Record<string, any>
45+
}

src/lib/utils.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Client } from '@modelcontextprotocol/sdk/client/index.js'
33
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js'
44
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'
55
import { Transport } from '@modelcontextprotocol/sdk/shared/transport.js'
6+
import type { MCPLogMessageParams } from './types'
67

78
// Connection constants
89
export const REASON_AUTH_NEEDED = 'authentication-needed'
@@ -487,3 +488,19 @@ export function setupSignalHandlers(cleanup: () => Promise<void>) {
487488
export function getServerUrlHash(serverUrl: string): string {
488489
return crypto.createHash('md5').update(serverUrl).digest('hex')
489490
}
491+
492+
/**
493+
* Helper function to send log messages through stdio MCP transport, for proxy logging purposes
494+
* @param transport The transport to send the message through
495+
* @param params Message parameters including level, logger name, and data
496+
*
497+
* In accordance with the official MCP specification
498+
* @see https://modelcontextprotocol.io/specification/2025-03-26/server/utilities/logging#log-message-notifications
499+
*/
500+
export function sendLog(transport: Transport, params: MCPLogMessageParams) {
501+
return transport.send({
502+
jsonrpc: '2.0',
503+
method: 'notifications/message',
504+
params: { ...params },
505+
})
506+
}

src/proxy.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
getServerUrlHash,
2121
MCP_REMOTE_VERSION,
2222
TransportStrategy,
23+
sendLog,
2324
} from './lib/utils'
2425
import { NodeOAuthClientProvider } from './lib/node-oauth-client-provider'
2526
import { createLazyAuthCoordinator } from './lib/coordination'
@@ -76,6 +77,14 @@ async function runProxy(
7677
}
7778
}
7879

80+
sendLog(localTransport, {
81+
level: 'debug',
82+
logger: 'mcp-remote',
83+
data: {
84+
message: 'Connecting to remote server...',
85+
},
86+
})
87+
7988
try {
8089
// Connect to remote server with lazy authentication
8190
const remoteTransport = await connectToRemoteServer(null, serverUrl, authProvider, headers, authInitializer, transportStrategy)

0 commit comments

Comments
 (0)