Skip to content
This repository was archived by the owner on May 21, 2026. It is now read-only.

Commit bb69c8d

Browse files
committed
feat: requests logs and timestamps
1 parent 2c52f1f commit bb69c8d

2 files changed

Lines changed: 54 additions & 5 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import type { Request, Response, NextFunction } from 'express'
2+
3+
/**
4+
* Request logging middleware with timestamp, colored status codes, and MCP method info
5+
*
6+
* Logs all HTTP requests with:
7+
* - Timestamp in HH:MM:SS.mmm format
8+
* - HTTP method and endpoint in bold
9+
* - MCP method name in brackets for POST requests to /mcp
10+
* - Color-coded status codes (green 2xx, yellow 3xx, red 4xx, magenta 5xx)
11+
*
12+
* @param req - Express request object
13+
* @param res - Express response object
14+
* @param next - Express next function
15+
*/
16+
export function requestLogger(req: Request, res: Response, next: NextFunction): void {
17+
const timestamp = new Date().toISOString().substring(11, 23)
18+
const method = req.method
19+
const url = req.url
20+
21+
// Override res.end to capture status code
22+
const originalEnd = res.end.bind(res)
23+
res.end = function(chunk?: any, encoding?: any, cb?: any) {
24+
const statusCode = res.statusCode
25+
let statusColor = ''
26+
27+
if (statusCode >= 200 && statusCode < 300) {
28+
statusColor = '\x1b[32m' // Green for 2xx
29+
} else if (statusCode >= 300 && statusCode < 400) {
30+
statusColor = '\x1b[33m' // Yellow for 3xx
31+
} else if (statusCode >= 400 && statusCode < 500) {
32+
statusColor = '\x1b[31m' // Red for 4xx
33+
} else if (statusCode >= 500) {
34+
statusColor = '\x1b[35m' // Magenta for 5xx
35+
}
36+
37+
// Add MCP method info for POST requests to /mcp
38+
let logMessage = `[${timestamp}] ${method} \x1b[1m${url}\x1b[0m`
39+
if (method === 'POST' && url === '/mcp' && req.body?.method) {
40+
logMessage += ` \x1b[1m[${req.body.method}]\x1b[0m`
41+
}
42+
logMessage += ` ${statusColor}${statusCode}\x1b[0m`
43+
44+
console.log(logMessage)
45+
46+
return originalEnd(chunk, encoding, cb)
47+
}
48+
49+
next()
50+
}

packages/mcp-use/src/server/mcp-server.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { z } from 'zod'
99
import express, { type Express } from 'express'
1010
import { existsSync, readdirSync } from 'node:fs'
1111
import { join } from 'node:path'
12+
import { requestLogger } from './logging.js'
1213

1314
export class McpServer {
1415
private server: OfficialMcpServer
@@ -45,6 +46,9 @@ export class McpServer {
4546
next()
4647
})
4748

49+
// Request logging middleware
50+
this.app.use(requestLogger)
51+
4852
// Setup default widget serving routes
4953
this.setupWidgetRoutes()
5054

@@ -237,21 +241,16 @@ export class McpServer {
237241

238242
// GET endpoint for SSE streaming
239243
this.app.get(endpoint, async (req, res) => {
240-
console.log(`[MCP] GET ${endpoint}`)
241244
await httpTransport.handleRequest(req, res)
242245
})
243246

244247
// POST endpoint for messages
245248
this.app.post(endpoint, express.json(), async (req, res) => {
246-
const method = req.body?.method || 'unknown'
247-
const id = req.body?.id || 'no-id'
248-
console.log(`[MCP] POST ${endpoint}${method} (id: ${id})`)
249249
await httpTransport.handleRequest(req, res, req.body)
250250
})
251251

252252
// DELETE endpoint for session cleanup
253253
this.app.delete(endpoint, async (req, res) => {
254-
console.log(`[MCP] DELETE ${endpoint}`)
255254
await httpTransport.handleRequest(req, res)
256255
})
257256

0 commit comments

Comments
 (0)