Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,42 @@ name: CI

on:
push:
branches: [ main ]
branches: [main]
pull_request:
branches: [ main ]
branches: [main]

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x, 22.x, 23.x]

steps:
- uses: actions/checkout@v4

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- name: Install pnpm
uses: pnpm/action-setup@v3
with:
version: 9

- name: Install dependencies
run: pnpm install


- name: Check Prettier
run: pnpm prettier:format:check

- name: Build
run: pnpm build

- name: Test
run: pnpm test

- name: Test Coverage
run: pnpm test:coverage
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ pnpm inspect
Executes commands in the specified shell.

Parameters:

- `command` (string, required): The shell command to execute
- `workingDir` (string, optional): The working directory to execute the command in. Must be under $HOME.

Expand Down Expand Up @@ -185,6 +186,7 @@ URI: `username://`
### system-info

Returns comprehensive system information in JSON format, including:

- hostname
- platform
- shell
Expand Down
73 changes: 43 additions & 30 deletions best_practice.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ src/
### Best Practices

1. **Avoid console output in MCP servers**

- Console output can interfere with the MCP protocol's stdout/stdin communication
- Use file-based logging instead

2. **Configure a proper logger**

```typescript
// lib/logger.ts
import winston from "winston";
Expand All @@ -40,14 +42,14 @@ src/
level: "info",
format: winston.format.combine(
winston.format.timestamp(),
winston.format.json()
winston.format.json(),
),
transports: [
new winston.transports.File({
filename: path.join(process.cwd(), "your-server.log")
})
new winston.transports.File({
filename: path.join(process.cwd(), "your-server.log"),
}),
// Console output is disabled to avoid interfering with MCP protocol communication
]
],
});
```

Expand All @@ -64,7 +66,7 @@ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"

const server = new McpServer({
name: "your-server-name",
version: "1.0.0"
version: "1.0.0",
});

// Configure tools, connect transports, etc.
Expand Down Expand Up @@ -95,18 +97,20 @@ server.tool(
// Define parameters with validation
required_param: z.string().min(1),
optional_param: z.number().optional(),
enum_param: z.enum(["option1", "option2", "option3"]).default("option1")
enum_param: z.enum(["option1", "option2", "option3"]).default("option1"),
},
async ({ required_param, optional_param, enum_param }) => {
// Tool implementation
// ...
return {
content: [{
type: "text",
text: "Result of operation"
}]
content: [
{
type: "text",
text: "Result of operation",
},
],
};
}
},
);
```

Expand All @@ -123,33 +127,39 @@ server.tool(
"example_tool",
"Description of what the tool does",
{
param: z.string()
param: z.string(),
},
async ({ param }) => {
try {
// Main operation
const result = await someOperation(param);

return {
content: [{
type: "text",
text: result
}]
content: [
{
type: "text",
text: result,
},
],
};
} catch (error) {
// Log the error
logger.error(`Error in example_tool: ${error instanceof Error ? error.message : String(error)}`);

logger.error(
`Error in example_tool: ${error instanceof Error ? error.message : String(error)}`,
);

// Return error response
return {
content: [{
type: "text",
text: `Error: ${error instanceof Error ? error.message : String(error)}`
}],
isError: true
content: [
{
type: "text",
text: `Error: ${error instanceof Error ? error.message : String(error)}`,
},
],
isError: true,
};
}
}
},
);
```

Expand Down Expand Up @@ -211,9 +221,7 @@ Set up npm scripts to facilitate testing:
"bin": {
"your-server-name": "dist/index.js"
},
"files": [
"dist"
],
"files": ["dist"],
"publishConfig": {
"access": "public"
},
Expand Down Expand Up @@ -252,11 +260,13 @@ Include:
## Security Considerations

1. **Input Validation**

- Always validate and sanitize user inputs
- Use Zod for parameter validation
- Be cautious with dynamic code execution

2. **Environment Variables**

- Use environment variables for secrets and configuration
- Provide clear documentation on required environment variables
- Validate environment variables at startup
Expand All @@ -268,6 +278,7 @@ Include:
## Performance

1. **Efficient Resource Usage**

- Close connections and free resources when done
- Be mindful of memory usage, especially for large responses
- Use streams for large data transfers when appropriate
Expand All @@ -280,6 +291,7 @@ Include:
## Monitoring and Debugging

1. **Structured Logging**

- Use structured logging format (JSON)
- Include context information in logs
- Log at appropriate levels (info, warn, error)
Expand All @@ -291,6 +303,7 @@ Include:
## Conclusion

By following these best practices, you'll create MCP servers that are:

- Robust and reliable
- Well-documented and maintainable
- Secure and performant
Expand All @@ -300,4 +313,4 @@ Remember that the Model Context Protocol is evolving, so stay updated with the l

---

© 2025 Your Name or Organization
© 2025 Your Name or Organization
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
"dev": "tsc -w",
"inspect": "mcp-inspector dist/index.js",
"prepublishOnly": "npm run build",
"prettier:format": "prettier --write .",
"prettier:format:check": "prettier --check .",
"test": "vitest run",
"test:watch": "vitest",
"test:coverage": "vitest run --coverage"
Expand All @@ -45,8 +47,9 @@
"@modelcontextprotocol/inspector": "^0.10.2",
"@types/node": "^22.14.1",
"@vitest/coverage-v8": "^3.1.1",
"prettier": "^3.5.3",
"shx": "^0.4.0",
"typescript": "^5.8.3",
"vitest": "^3.1.1"
}
}
}
Loading