mrapids 0.1.7

Your OpenAPI, but executable
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# MCP Integration Testing Guide

## Prerequisites

1. Build the project:
```bash
cd /Users/neetagundala/Projects/microrapid_runtime/api-runtime
cargo build --release
```

2. Build the MCP server:
```bash
cd agent
cargo build --release
```

## 1. Testing the MCP Server

### Start the Server

```bash
# Generate example configuration
./target/release/mrapids-agent --generate-config > test-mcp-server.toml

# Create test directory structure
mkdir -p test-mcp
cd test-mcp
mkdir -p .mrapids/auth

# Start the server with example config
../target/release/mrapids-agent --config ../test-mcp-server.toml --config-dir .mrapids
```

The server will create example files in `.mrapids/` including:
- `policy.yaml` - Default safety policy
- `auth/` - Example auth profiles
- `api.yaml` - Example OpenAPI spec

### Test JSON-RPC Endpoints

1. **Health Check**:
```bash
curl -X POST http://localhost:8080 \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "method": "health",
    "params": {},
    "id": 1
  }'
```

Expected response:
```json
{
  "jsonrpc": "2.0",
  "result": {
    "status": "healthy",
    "version": "0.1.0",
    "service": "mrapids-agent"
  },
  "id": 1
}
```

2. **List Operations**:
```bash
curl -X POST http://localhost:8080 \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "method": "tools/list",
    "params": {
      "filter": {
        "method": "GET"
      }
    },
    "id": 2
  }'
```

3. **Show Operation Details**:
```bash
curl -X POST http://localhost:8080 \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "method": "tools/show",
    "params": {
      "operation_id": "health"
    },
    "id": 3
  }'
```

## 2. Testing with Real APIs

### Setup GitHub API Example

1. Create a GitHub API spec:
```bash
cat > .mrapids/github-api.yaml << 'EOF'
openapi: "3.0.0"
info:
  title: GitHub API
  version: "1.0.0"
servers:
  - url: https://api.github.com
paths:
  /user:
    get:
      operationId: getAuthenticatedUser
      summary: Get the authenticated user
      security:
        - bearerAuth: []
      responses:
        200:
          description: User details
  /users/{username}:
    get:
      operationId: getUser
      summary: Get a user by username
      parameters:
        - name: username
          in: path
          required: true
          schema:
            type: string
      responses:
        200:
          description: User details
  /repos/{owner}/{repo}:
    get:
      operationId: getRepository
      summary: Get repository information
      parameters:
        - name: owner
          in: path
          required: true
          schema:
            type: string
        - name: repo
          in: path
          required: true
          schema:
            type: string
      responses:
        200:
          description: Repository details
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
EOF
```

2. Create auth profile for GitHub:
```bash
cat > .mrapids/auth/github.toml << 'EOF'
[profile]
name = "github"
type = "bearer"
token_env = "GITHUB_TOKEN"
EOF
```

3. Update server config to use GitHub API:
```toml
# In test-mcp-server.toml, update:
[defaults]
spec_path = ".mrapids/github-api.yaml"
auth_profile = "github"
```

4. Set GitHub token (read-only):
```bash
export GITHUB_TOKEN="your-github-token"
```

5. Test GitHub API operations:
```bash
# List GitHub operations
curl -X POST http://localhost:8080 \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "method": "tools/list",
    "params": {},
    "id": 4
  }'

# Get repository info
curl -X POST http://localhost:8080 \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "method": "tools/run",
    "params": {
      "operation_id": "getRepository",
      "parameters": {
        "owner": "anthropics",
        "repo": "anthropic-sdk-python"
      }
    },
    "id": 5
  }'
```

## 3. Testing Policy Engine

### Create Test Policy

```bash
cat > .mrapids/test-policy.yaml << 'EOF'
version: "1.0"
metadata:
  name: "test-policy"
  description: "Policy for testing"

defaults:
  allow_methods: ["GET"]
  deny_external_refs: true
  require_auth: false
  audit_level: "detailed"

rules:
  - name: "allow-public-reads"
    description: "Allow read operations on public endpoints"
    pattern: "*/users/*"
    allow:
      methods: ["GET"]
      
  - name: "deny-private-endpoints"
    description: "Deny access to private endpoints"
    pattern: "*/admin/*"
    deny:
      reason: "Admin endpoints are not accessible to agents"
      
  - name: "require-auth-for-user"
    description: "Require auth for authenticated user endpoint"
    pattern: "*/user"
    conditions:
      - type: "has_auth"
        value: "true"
    allow:
      methods: ["GET"]
EOF
```

Update server to use test policy and restart.

### Test Policy Enforcement

1. **Test allowed operation**:
```bash
curl -X POST http://localhost:8080 \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "method": "tools/run",
    "params": {
      "operation_id": "getUser",
      "parameters": {
        "username": "octocat"
      }
    },
    "id": 6
  }'
```

2. **Test denied operation** (if you add an admin endpoint):
Should return error code 1001 (PolicyDeny).

## 4. Testing Audit Logging

Check audit logs:
```bash
# Logs are in the audit directory
ls -la .mrapids/audit/

# View recent audit entries
tail -f .mrapids/audit/mcp-audit-*.log | jq .
```

Each log entry contains:
- Unique ID
- Timestamp
- Operation details
- Policy decision
- Response (if detailed audit level)

## 5. Testing Response Redaction

1. Create a test endpoint that returns sensitive data:
```yaml
/test/secrets:
  get:
    operationId: getSecrets
    responses:
      200:
        description: Test response with secrets
        content:
          application/json:
            example:
              api_key: "sk-1234567890abcdef"
              token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
              password: "super-secret-password"
              safe_data: "this is safe"
```

2. Call the endpoint and verify redaction:
```bash
curl -X POST http://localhost:8080 \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "method": "tools/run",
    "params": {
      "operation_id": "getSecrets"
    },
    "id": 7
  }'
```

Response should show:
```json
{
  "api_key": "[REDACTED:API_KEY]",
  "token": "[REDACTED:JWT]",
  "password": "[REDACTED:PASSWORD]",
  "safe_data": "this is safe"
}
```

## 6. Integration Testing with Python

```python
import requests
import json

class MCPClient:
    def __init__(self, url="http://localhost:8080"):
        self.url = url
        self.id_counter = 1
        
    def call(self, method, params=None):
        payload = {
            "jsonrpc": "2.0",
            "method": method,
            "params": params or {},
            "id": self.id_counter
        }
        self.id_counter += 1
        
        response = requests.post(self.url, json=payload)
        return response.json()

# Test the client
client = MCPClient()

# List operations
result = client.call("tools/list", {"filter": {"method": "GET"}})
print("Operations:", json.dumps(result, indent=2))

# Show operation details
result = client.call("tools/show", {"operation_id": "getUser"})
print("Operation details:", json.dumps(result, indent=2))

# Execute operation
result = client.call("tools/run", {
    "operation_id": "getUser",
    "parameters": {"username": "octocat"}
})
print("Execution result:", json.dumps(result, indent=2))
```

## 7. Performance Testing

```bash
# Install Apache Bench
# macOS: already installed
# Linux: apt-get install apache2-utils

# Test concurrent requests
ab -n 1000 -c 10 -p request.json -T application/json http://localhost:8080/
```

Where `request.json` contains:
```json
{
  "jsonrpc": "2.0",
  "method": "tools/list",
  "params": {},
  "id": 1
}
```

## 8. Security Testing

1. **Test SQL injection attempts**:
```bash
curl -X POST http://localhost:8080 \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "method": "tools/run",
    "params": {
      "operation_id": "getUser",
      "parameters": {
        "username": "octocat'; DROP TABLE users;--"
      }
    },
    "id": 8
  }'
```

2. **Test path traversal**:
```bash
curl -X POST http://localhost:8080 \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "method": "tools/run",
    "params": {
      "operation_id": "../../etc/passwd"
    },
    "id": 9
  }'
```

## 9. Unit Tests

Run the existing unit tests:
```bash
# Run all tests
cargo test

# Run with output
cargo test -- --nocapture

# Run specific test modules
cargo test policy::
cargo test api::
cargo test audit::
```

## 10. Debugging Tips

1. **Enable debug logging**:
```bash
RUST_LOG=debug ./target/release/mrapids-agent --config test-mcp-server.toml
```

2. **Check server state**:
- Audit logs show all operations
- Server logs show policy decisions
- Use `--generate-config` to see all config options

3. **Common issues**:
- Missing auth token: Set environment variables
- Policy denials: Check policy rules and audit logs
- Connection refused: Ensure server is running on correct port

This comprehensive testing approach covers unit tests, integration tests, security tests, and performance tests for the MCP integration.