|
| 1 | +# ValTown MCP Server Code Conventions |
| 2 | + |
| 3 | +This document outlines the coding standards, architecture, and conventions used |
| 4 | +in the ValTown MCP Server project. It serves as both an onboarding guide for new |
| 5 | +developers and a reference for AI coding assistants. |
| 6 | + |
| 7 | +## Project Structure |
| 8 | + |
| 9 | +``` |
| 10 | +valTownMCPServer/ |
| 11 | +├── lib/ # Core utilities and types |
| 12 | +├── tools/ # MCP tools implementations |
| 13 | +├── mod.ts # Main entry point |
| 14 | +├── server.ts # Server implementation |
| 15 | +└── docs/ # Documentation |
| 16 | +``` |
| 17 | + |
| 18 | +## TypeScript Conventions |
| 19 | + |
| 20 | +### General Style |
| 21 | + |
| 22 | +- Use TypeScript strict mode |
| 23 | +- Prefer explicit typing over inferred types for function parameters and returns |
| 24 | +- Use interfaces for object shapes and types for unions/aliases |
| 25 | +- Indent with 2 spaces |
| 26 | +- Use semicolons at the end of statements |
| 27 | + |
| 28 | +### Naming Conventions |
| 29 | + |
| 30 | +- `camelCase` for variables, functions, and method names |
| 31 | +- `PascalCase` for interface, type, class, and enum names |
| 32 | +- `UPPER_SNAKE_CASE` for constants |
| 33 | +- Descriptive names that indicate purpose (avoid abbreviations) |
| 34 | + |
| 35 | +### Example |
| 36 | + |
| 37 | +```typescript |
| 38 | +// Good |
| 39 | +interface UserResponse { |
| 40 | + id: string; |
| 41 | + username: string; |
| 42 | +} |
| 43 | + |
| 44 | +async function fetchUserData(userId: string): Promise<UserResponse> { |
| 45 | + // Implementation |
| 46 | +} |
| 47 | + |
| 48 | +// Avoid |
| 49 | +interface resp { |
| 50 | + id: string; |
| 51 | + uname: string; |
| 52 | +} |
| 53 | + |
| 54 | +async function fetch(id) { |
| 55 | + // Implementation |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +## MCP Tool Conventions |
| 60 | + |
| 61 | +### Tool Implementation |
| 62 | + |
| 63 | +- Each tool should have a clear, specific purpose |
| 64 | +- Tool names should be kebab-case (e.g., `get-val`, `run-function`) |
| 65 | +- Tool descriptions should be concise but comprehensive |
| 66 | +- Parameters should have descriptive names and appropriate validation |
| 67 | +- Tools should handle errors gracefully |
| 68 | +- Return consistent response formats |
| 69 | + |
| 70 | +### Example Tool Registration |
| 71 | + |
| 72 | +```typescript |
| 73 | +server.tool( |
| 74 | + "run-function", |
| 75 | + "Execute a ValTown function by its ID or URL", |
| 76 | + { |
| 77 | + functionIdentifier: z.string().describe("Function ID or URL to execute"), |
| 78 | + args: z.array(z.any()).optional().describe( |
| 79 | + "Arguments to pass to the function", |
| 80 | + ), |
| 81 | + }, |
| 82 | + async ({ functionIdentifier, args = [] }) => { |
| 83 | + try { |
| 84 | + // Implementation |
| 85 | + return { |
| 86 | + content: [{ type: "text", text: "Result data" }], |
| 87 | + }; |
| 88 | + } catch (error) { |
| 89 | + return { |
| 90 | + content: [{ type: "text", text: `Error: ${getErrorMessage(error)}` }], |
| 91 | + isError: true, |
| 92 | + }; |
| 93 | + } |
| 94 | + }, |
| 95 | +); |
| 96 | +``` |
| 97 | + |
| 98 | +## Error Handling |
| 99 | + |
| 100 | +- Use try/catch blocks for all async operations |
| 101 | +- Use the `getErrorMessage` utility for consistent error message extraction |
| 102 | +- Return appropriate error responses with isError flag set to true |
| 103 | +- Log errors with relevant context |
| 104 | + |
| 105 | +## Testing Conventions |
| 106 | + |
| 107 | +- Unit tests for individual functions |
| 108 | +- Integration tests for API interactions |
| 109 | +- Test files should be named `*.test.ts` |
| 110 | +- Use descriptive test names that explain the expected behavior |
| 111 | + |
| 112 | +## Version Control |
| 113 | + |
| 114 | +- Commit messages should follow |
| 115 | + [Conventional Commits](https://www.conventionalcommits.org/) format |
| 116 | +- Feature branches should follow the pattern `feature/description-of-feature` |
| 117 | +- Bug fix branches should follow the pattern `fix/description-of-bug` |
| 118 | + |
| 119 | +## Documentation |
| 120 | + |
| 121 | +- Document all public functions and interfaces with JSDoc comments |
| 122 | +- Keep README.md up to date with installation and usage instructions |
| 123 | +- Add examples for common use cases |
| 124 | + |
| 125 | +## Environment Variables |
| 126 | + |
| 127 | +- Document all required environment variables |
| 128 | +- Use sensible defaults when possible |
| 129 | +- Validate environment variables at startup |
| 130 | + |
| 131 | +## Performance Considerations |
| 132 | + |
| 133 | +- Minimize external API calls |
| 134 | +- Use appropriate caching strategies |
| 135 | +- Handle rate limiting gracefully |
| 136 | + |
| 137 | +## Security Practices |
| 138 | + |
| 139 | +- Never commit API tokens or secrets |
| 140 | +- Validate and sanitize all user inputs |
| 141 | +- Follow the principle of least privilege |
| 142 | + |
| 143 | +``` |
| 144 | +By following these conventions, we maintain a consistent, high-quality codebase that is easier to maintain and extend. |
| 145 | +``` |
0 commit comments