summaryrefslogtreecommitdiff
path: root/mcp-servers/simple-mcp-server/.claude/commands/init.md
blob: 885af944a65d0bcea47ddee9b0eb77288d3cd19d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# Initialize MCP Server Project

Sets up a new MCP server project with the specified configuration level.

## Usage

```
/init [basic|standard|full]
```

## Options

- `basic` - Minimal server with one example tool
- `standard` - Server with tools and resources (default)
- `full` - Complete server with all capabilities

## Implementation

```typescript
async function initializeProject(level: 'basic' | 'standard' | 'full' = 'standard') {
  // Create project structure
  const dirs = [
    'src',
    'src/tools',
    'src/resources',
    'src/prompts',
    'src/utils',
    'src/types',
    'tests',
    'tests/unit',
    'tests/integration',
  ];
  
  for (const dir of dirs) {
    await fs.mkdir(dir, { recursive: true });
  }
  
  // Create package.json
  const packageJson = {
    name: 'mcp-server',
    version: '1.0.0',
    type: 'module',
    scripts: {
      'dev': 'tsx watch src/index.ts',
      'build': 'tsc',
      'start': 'node dist/index.js',
      'test': 'vitest',
      'lint': 'eslint src',
      'typecheck': 'tsc --noEmit',
    },
    dependencies: {
      '@modelcontextprotocol/sdk': '^1.0.0',
      'zod': '^3.22.0',
    },
    devDependencies: {
      '@types/node': '^20.0.0',
      'typescript': '^5.0.0',
      'tsx': '^4.0.0',
      'vitest': '^1.0.0',
      'eslint': '^8.0.0',
    },
  };
  
  await fs.writeFile('package.json', JSON.stringify(packageJson, null, 2));
  
  // Create tsconfig.json
  const tsConfig = {
    compilerOptions: {
      target: 'ES2022',
      module: 'NodeNext',
      moduleResolution: 'NodeNext',
      outDir: './dist',
      rootDir: './src',
      strict: true,
      esModuleInterop: true,
      skipLibCheck: true,
      forceConsistentCasingInFileNames: true,
    },
    include: ['src/**/*'],
    exclude: ['node_modules', 'dist'],
  };
  
  await fs.writeFile('tsconfig.json', JSON.stringify(tsConfig, null, 2));
  
  // Create main server file
  let serverContent = '';
  
  if (level === 'basic') {
    serverContent = generateBasicServer();
  } else if (level === 'standard') {
    serverContent = generateStandardServer();
  } else {
    serverContent = generateFullServer();
  }
  
  await fs.writeFile('src/index.ts', serverContent);
  
  // Install dependencies
  console.log('Installing dependencies...');
  await exec('npm install');
  
  console.log('✅ MCP server initialized successfully!');
  console.log('\nNext steps:');
  console.log('1. Run "npm run dev" to start development server');
  console.log('2. Use "/add-tool" to add custom tools');
  console.log('3. Test with MCP Inspector: npx @modelcontextprotocol/inspector');
}

function generateBasicServer(): string {
  return `
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import {
  CallToolRequestSchema,
  ListToolsRequestSchema,
} from '@modelcontextprotocol/sdk/types.js';
import { z } from 'zod';

const server = new Server({
  name: 'my-mcp-server',
  version: '1.0.0',
}, {
  capabilities: {
    tools: {},
  },
});

// List available tools
server.setRequestHandler(ListToolsRequestSchema, async () => {
  return {
    tools: [
      {
        name: 'hello',
        description: 'Say hello to someone',
        inputSchema: {
          type: 'object',
          properties: {
            name: {
              type: 'string',
              description: 'Name to greet',
            },
          },
          required: ['name'],
        },
      },
    ],
  };
});

// Handle tool calls
server.setRequestHandler(CallToolRequestSchema, async (request) => {
  const { name, arguments: args } = request.params;
  
  if (name === 'hello') {
    const validated = z.object({
      name: z.string(),
    }).parse(args);
    
    return {
      content: [
        {
          type: 'text',
          text: \`Hello, \${validated.name}!\`,
        },
      ],
    };
  }
  
  throw new Error(\`Unknown tool: \${name}\`);
});

// Start server
const transport = new StdioServerTransport();
await server.connect(transport);
console.error('MCP server running on stdio');
`;
}
```