pmcp 1.8.4

High-quality Rust SDK for Model Context Protocol (MCP) with full TypeScript SDK compatibility
Documentation
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# MCP Server Development Tutorial

Learn MCP server development through progressive discovery. This tutorial takes you from basic concepts to advanced features in three phases.

## Prerequisites

- Rust installed (1.70 or later)
- Claude Code or another MCP client
- 30 minutes

## Learning Path Overview

| Phase | Template | What You'll Learn | Duration |
|-------|----------|-------------------|----------|
| 1 | Simple Calculator | Basic MCP: tools, requests, responses | 10 min |
| 2 | Complete Calculator | Advanced: validation, errors, resources, prompts | 10 min |
| 3 | SQLite Explorer | Expert: workflows, step bindings, databases | 10 min |

---

## Phase 1: Your First MCP Server (Simple Calculator)

**Goal**: Understand basic MCP concepts with a single `add` tool.

### Step 1: Create Workspace

```bash
cargo pmcp new my-mcp-learning
cd my-mcp-learning
```

This creates a workspace with `server-common` (shared HTTP bootstrap).

### Step 2: Add Simple Calculator Server

```bash
cargo pmcp add server calculator --template calculator
```

**What happened:**
- Created `mcp-calculator-core` (business logic)
- Created `calculator-server` (HTTP binary)
- Assigned port 3000 automatically
- Saved configuration to `.pmcp-config.toml`

### Step 3: Start the Server

```bash
cargo pmcp dev --server calculator
```

**What to observe:**
- Server builds and starts on port 3000
- Shows "Server URL: http://0.0.0.0:3000"
- Logs show MCP server is ready

### Step 4: Connect to Claude Code

In a **new terminal** (keep server running):

```bash
cargo pmcp connect --server calculator --client claude-code
```

This configures Claude Code to connect to your server.

### Step 5: Test the `add` Tool

In Claude Code, try:
- "Add 5 and 3"
- "What's 42 plus 17?"
- "Calculate the sum of 123 and 456"

**What's happening:**
1. Claude Code sends MCP request to your server
2. Server receives JSON-RPC request for `add` tool
3. Tool executes: `{ a: 5, b: 3 }``{ result: 8 }`
4. Claude Code receives response and answers

### Step 6: Explore the Code

Open `crates/mcp-calculator-core/src/lib.rs` and examine:

```rust
// Tool Input - Auto-generates JSON schema
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct AddInput {
    pub a: f64,
    pub b: f64,
}

// Tool Handler - Async function
async fn add_tool(input: AddInput, _extra: RequestHandlerExtra) -> Result<AddResult> {
    Ok(AddResult {
        result: input.a + input.b,
    })
}

// Server Registration
Server::builder()
    .tool("add", TypedTool::new("add", |input, extra| {
        Box::pin(add_tool(input, extra))
    }))
    .build()
```

**Key Concepts:**
- **Tools** = Functions Claude can call
- **TypedTool** = Type-safe tool with automatic JSON schema
- **Input/Output** = Defined by Rust types (validated automatically)

### Checkpoint: What You've Learned ✓

- [x] MCP servers provide tools to AI agents
- [x] Tools have typed inputs and outputs
- [x] `cargo-pmcp` handles HTTP bootstrap
- [x] Servers run on specific ports (3000 by default)

Stop the server (Ctrl+C) and proceed to Phase 2.

---

## Phase 2: Level Up (Complete Calculator)

**Goal**: Learn advanced patterns—multiple tools, validation, resources, prompts.

### Step 1: Upgrade Your Server

```bash
cargo pmcp add server calculator --template complete-calculator --replace
```

**What happened:**
- Prompted for confirmation (shows current vs new template)
- Deleted old `mcp-calculator-core` and `calculator-server`
- Generated new crates with complete template
- **Kept port 3000** (no config change needed in Claude Code!)

### Step 2: Restart the Server

```bash
cargo pmcp dev --server calculator
```

Server starts on **same port** (3000)—Claude Code reconnects automatically!

### Step 3: Test Multiple Tools

In Claude Code, try:
- "Multiply 7 by 8"
- "Divide 100 by 4"
- "What's 2 to the power of 10?"
- "Calculate the square root of 144"

**New capabilities:**
- `add`, `subtract`, `multiply` (basic arithmetic)
- `divide` (with zero-division validation!)
- `power` (exponentiation)
- `sqrt` (square root)

### Step 4: Test Error Handling

Try: "Divide 10 by 0"

Observe Claude's response—the server returned a validation error!

```rust
// In divide_tool():
if input.b == 0.0 {
    return Err(Error::validation("Cannot divide by zero"));
}
```

### Step 5: Try a Prompt (Workflow)

In Claude Code, use the `/prompts` command:
```
/quadratic a: 1 b: -5 c: 6
```

**What's happening:**
1. Prompt orchestrates multiple tools
2. Calculates discriminant (b² - 4ac)
3. Determines if real roots exist
4. Calculates roots using quadratic formula
5. Returns step-by-step solution

### Step 6: Explore Resources

Ask Claude: "Show me the quadratic formula guide"

**Resources provide:**
- Static knowledge/documentation
- Context for the AI
- Educational material

### Checkpoint: What You've Learned ✓

- [x] Servers can have multiple tools
- [x] Validation returns errors to the client
- [x] Prompts orchestrate multi-step workflows
- [x] Resources provide static knowledge
- [x] `--replace` upgrades servers in-place

Stop the server and proceed to Phase 3.

---

## Phase 3: Advanced Features (SQLite Explorer)

**Goal**: Master workflows with step bindings, resources, and real databases.

### Step 1: Add Database Server

```bash
cargo pmcp add server explorer --template sqlite-explorer
```

**What happened:**
- Created explorer server
- **Auto-assigned port 3001** (next available)
- Both servers in workspace:
  - `calculator` (port 3000)
  - `explorer` (port 3001)

### Step 2: Download Sample Database

```bash
cd crates/mcp-explorer-core
curl -L https://github.com/lerocha/chinook-database/raw/master/ChinookDatabase/DataSources/Chinook_Sqlite.sqlite -o chinook.db
cd ../..
```

This downloads the Chinook database (music store with 11 tables, 3500+ tracks).

### Step 3: Start Explorer Server

```bash
cargo pmcp dev --server explorer
```

Server starts on port 3001.

### Step 4: Connect Explorer to Claude Code

In a **new terminal**:

```bash
cargo pmcp connect --server explorer --client claude-code
```

**Now you have TWO servers configured in Claude Code:**
- `calculator` (port 3000) - Off
- `explorer` (port 3001) - Running

### Step 5: Explore the Database

In Claude Code, try:
- "Show me all tables in the database"
- "Get sample rows from the Track table"
- "List customers and their countries"

**Tools available:**
- `execute_query` - Run SELECT queries (read-only)
- `list_tables` - Show all tables with row counts
- `get_sample_rows` - Preview table data

### Step 6: Use Advanced Workflows

Try the workflow prompts:

```
/monthly_sales_report month: 3 year: 2021
```

**What's different from Phase 2:**
- Workflows use SQL template substitution: `WHERE month = {month}`
- Multi-step workflows with bindings: Step 1 output → Step 2 input
- Real database queries (not just calculations)

```
/analyze_customer customer_id: 5
```

This runs a **3-step workflow**:
1. Get customer info
2. Get purchase history
3. Calculate lifetime value

All results are bound and composed automatically!

### Step 7: Multi-Step Workflow

```
/customers_who_bought_top_tracks limit: 10
```

**How it works:**
1. Step 1: Query top 10 tracks by purchase count → binds as `top_tracks`
2. Client (Claude) receives `top_tracks` binding
3. Claude extracts track IDs
4. Claude generates SQL to find customers
5. Claude calls `execute_query` with generated SQL

This demonstrates **client-side orchestration**!

### Step 8: Run Both Servers

You can run both servers simultaneously:

Terminal 1:
```bash
cargo pmcp dev --server calculator
```

Terminal 2:
```bash
cargo pmcp dev --server explorer
```

Both servers run on different ports—Claude Code can use both at once!

### Checkpoint: What You've Learned ✓

- [x] Multiple servers can coexist (different ports)
- [x] Workflows can use template substitution
- [x] Multi-step workflows with bindings
- [x] Resources provide database schemas
- [x] Real-world use case: database operations

---

## Summary: Your MCP Journey

| Concept | Phase 1 | Phase 2 | Phase 3 |
|---------|---------|---------|---------|
| **Tools** | 1 (add) | 6 (arithmetic) | 3 (database ops) |
| **Validation** | None | Division by zero | SQL safety |
| **Prompts** | None | Quadratic solver | 3 workflows |
| **Resources** | None | Formula guide | DB schema |
| **Bindings** | No | No | Yes |
| **Complexity** | ⭐ Simple | ⭐⭐ Intermediate | ⭐⭐⭐ Advanced |

## Next Steps

### Customize Your Servers

1. **Add a new tool** to calculator:
   ```rust
   // In mcp-calculator-core/src/lib.rs
   async fn factorial_tool(input: FactorialInput, _extra: RequestHandlerExtra) -> Result<FactorialResult> {
       // Your implementation
   }
   ```

2. **Create a custom workflow** for explorer:
   ```rust
   fn create_genre_analysis_workflow() -> SequentialWorkflow {
       // Your workflow
   }
   ```

3. **Build your own server** from scratch:
   ```bash
   cargo pmcp add server my-server --template minimal
   ```

### Production Deployment

See the [Deployment Guide](./DEPLOYMENT.md) for:
- Docker containerization
- Environment configuration
- Production logging
- Monitoring and metrics

### Advanced Topics

- **Authentication**: Add auth middleware
- **Rate Limiting**: Protect your APIs
- **Custom Transport**: Beyond HTTP
- **Testing**: Write integration tests

## Troubleshooting

### Port Already in Use

```bash
Error: Port 3000 is already in use
```

**Solution**: Stop the other server or use a different port:
```bash
cargo pmcp dev --server calculator --port 3005
```

### Server Not Showing in Claude Code

1. Check `.pmcp-config.toml` has correct port
2. Restart Claude Code
3. Run connect command again:
   ```bash
   cargo pmcp connect --server explorer --client claude-code
   ```

### Template Substitution Not Working

Make sure you're using the latest version:
```bash
cargo install --path /path/to/rust-mcp-sdk/cargo-pmcp --force
```

## Resources

- [MCP Specification]https://spec.modelcontextprotocol.io/
- [PMCP SDK Documentation]https://docs.rs/pmcp
- [Example Servers]./examples/
- [API Reference]https://docs.rs/pmcp/latest/pmcp/

## Feedback

Found an issue or have a suggestion?
- [Open an issue]https://github.com/paiml/rust-mcp-sdk/issues
- [Discussions]https://github.com/paiml/rust-mcp-sdk/discussions

---

**Congratulations!** 🎉 You've completed the MCP Server Development Tutorial. You now understand:
- Basic MCP architecture (tools, prompts, resources)
- Progressive server development (simple → complete → advanced)
- Multi-server workspaces with port management
- Production patterns with cargo-pmcp

Happy building! 🚀