Skip to content

Commit f2147bd

Browse files
committed
feat: add structured logging with Pino and environment configuration
- Introduced `src/logger.ts` for structured logging using Pino, with support for OpenTelemetry trace correlation. - Updated `src/index.ts` to utilize the new logger for handling MCP requests and server operations. - Added environment configuration in `src/config.ts` with validation using Zod. - Enhanced error handling and logging throughout the application. - Updated documentation in `.cursorrules` and `CLAUDE.md` to reflect new features and usage instructions. - Added support for environment variables including `PORT`, `NODE_ENV`, `SERVER_NAME`, `SERVER_VERSION`, and `LOG_LEVEL`.
1 parent 27c95c7 commit f2147bd

File tree

6 files changed

+552
-62
lines changed

6 files changed

+552
-62
lines changed

.cursorrules

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ This is a TypeScript template for building Model Context Protocol (MCP) servers.
3636
- Defines all available MCP tools with their JSON schemas
3737
- Routes tool calls to registered tool handlers
3838
- Handles error responses in MCP format
39+
- **`src/config.ts`** - Environment configuration with validation using Zod
40+
- **`src/logger.ts`** - Structured logging with Pino (OpenTelemetry compatible)
41+
- **`src/lib/utils.ts`** - Utility functions for MCP response formatting
3942

4043
### Template MCP Tools Available
4144

@@ -73,16 +76,30 @@ The template includes one example tool to demonstrate MCP tool implementation:
7376
- Uses Express for reliable HTTP handling with excellent TypeScript support
7477
- Session management handles MCP initialization and transport lifecycle
7578
- Error handling returns MCP-formatted error messages rather than throwing
79+
- **Structured Logging**: Uses Pino for production-ready logging with OpenTelemetry trace correlation
80+
- **Configuration Management**: Environment variables validated with Zod schemas
81+
- **Graceful Shutdown**: Proper SIGTERM/SIGINT handling for container environments
7682

7783
## Template Usage
7884

7985
This is a template project for creating new MCP servers. To customize:
8086

8187
1. Update `package.json` with your project name and description
82-
2. Replace the echo tool in `src/index.ts` with your custom tools
83-
3. Add additional TypeScript files for business logic as needed
84-
4. Update README.md to document your specific MCP server functionality
85-
5. Modify this .cursorrules file to reflect your project's architecture
88+
2. Update environment variables in `src/config.ts` (SERVER_NAME, SERVER_VERSION, etc.)
89+
3. Replace the echo tool in `src/index.ts` with your custom tools
90+
4. Add additional TypeScript files for business logic as needed
91+
5. Update README.md to document your specific MCP server functionality
92+
6. Modify this .cursorrules file to reflect your project's architecture
93+
94+
## Environment Variables
95+
96+
The following environment variables are supported (see `src/config.ts`):
97+
98+
- `PORT` - Server port (default: 3000)
99+
- `NODE_ENV` - Environment (development/production/test)
100+
- `SERVER_NAME` - MCP server name (default: mcp-typescript-template)
101+
- `SERVER_VERSION` - Server version (default: 1.0.0)
102+
- `LOG_LEVEL` - Logging level (error/warn/info/debug, default: info)
86103

87104
## Coding Guidelines
88105

@@ -91,4 +108,15 @@ This is a template project for creating new MCP servers. To customize:
91108
- Prefer functional programming patterns where appropriate
92109
- Keep methods focused and single-responsibility
93110
- Use meaningful variable and function names
94-
- Add JSDoc comments for public APIs
111+
- Add JSDoc comments for public APIs
112+
- **Use structured logging**: Always use the `logger` from `src/logger.ts` instead of `console.log`
113+
- **Environment variables**: Add new config to `src/config.ts` with Zod validation
114+
- **Error handling**: Use try-catch blocks and log errors with context using structured logging
115+
116+
## Logging Best Practices
117+
118+
- Use appropriate log levels: `error`, `warn`, `info`, `debug`
119+
- Include relevant context in log messages (user IDs, session IDs, etc.)
120+
- Log structured data as the second parameter: `logger.info("message", { key: value })`
121+
- Error logs should include error details: `logger.error("Error message", { error: error.message })`
122+
- The logger automatically includes trace correlation when OpenTelemetry is configured

CLAUDE.md

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ This is a TypeScript template for building Model Context Protocol (MCP) servers.
3232
- Defines all available MCP tools with their JSON schemas
3333
- Routes tool calls to registered tool handlers
3434
- Handles error responses in MCP format
35+
- **`src/config.ts`** - Environment configuration with validation using Zod
36+
- **`src/logger.ts`** - Structured logging with Pino (OpenTelemetry compatible)
37+
- **`src/lib/utils.ts`** - Utility functions for MCP response formatting
3538

3639
### Template MCP Tools Available
3740

@@ -61,16 +64,39 @@ The template includes one example tool to demonstrate MCP tool implementation:
6164
- Uses Express for reliable HTTP handling with excellent TypeScript support
6265
- Session management handles MCP initialization and transport lifecycle
6366
- Error handling returns MCP-formatted error messages rather than throwing
67+
- **Structured Logging**: Uses Pino for production-ready logging with OpenTelemetry trace correlation
68+
- **Configuration Management**: Environment variables validated with Zod schemas
69+
- **Graceful Shutdown**: Proper SIGTERM/SIGINT handling for container environments
6470

6571
## Template Usage
6672

6773
This is a template project for creating new MCP servers. To customize:
6874

6975
1. Update `package.json` with your project name and description
70-
2. Replace the echo tool in `src/index.ts` with your custom tools
71-
3. Add additional TypeScript files for business logic as needed
72-
4. Update README.md to document your specific MCP server functionality
73-
5. Modify this CLAUDE.md file to reflect your project's architecture
76+
2. Update environment variables in `src/config.ts` (SERVER_NAME, SERVER_VERSION, etc.)
77+
3. Replace the echo tool in `src/index.ts` with your custom tools
78+
4. Add additional TypeScript files for business logic as needed
79+
5. Update README.md to document your specific MCP server functionality
80+
6. Modify this CLAUDE.md file to reflect your project's architecture
81+
82+
## Environment Variables
83+
84+
The following environment variables are supported (see `src/config.ts`):
85+
86+
- `PORT` - Server port (default: 3000)
87+
- `NODE_ENV` - Environment (development/production/test)
88+
- `SERVER_NAME` - MCP server name (default: mcp-typescript-template)
89+
- `SERVER_VERSION` - Server version (default: 1.0.0)
90+
- `LOG_LEVEL` - Logging level (error/warn/info/debug, default: info)
91+
92+
## Logging Best Practices
93+
94+
- Use appropriate log levels: `error`, `warn`, `info`, `debug`
95+
- Include relevant context in log messages (user IDs, session IDs, etc.)
96+
- Log structured data as the second parameter: `logger.info("message", { key: value })`
97+
- Error logs should include error details: `logger.error("Error message", { error: error.message })`
98+
- The logger automatically includes trace correlation when OpenTelemetry is configured
99+
- Use the `logger` from `src/logger.ts` instead of `console.log`
74100

75101
## Adding New Tools
76102

@@ -80,4 +106,6 @@ When adding new tools to the MCP server:
80106
2. Provide a clear title and description
81107
3. Define input schema using Zod for validation
82108
4. Return responses in MCP content format with JSON stringified data
83-
5. Handle errors gracefully and return appropriate error messages
109+
5. Handle errors gracefully and return appropriate error messages
110+
6. Use structured logging to track tool usage: `logger.info("Tool executed", { toolName, args })`
111+
7. Log errors with context: `logger.error("Tool execution failed", { toolName, error: error.message })`

0 commit comments

Comments
 (0)