mrapids 0.1.31

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
# MCP Integration User Guide

## Overview

MicroRapid's MCP (Model Context Protocol) integration enables AI agents to safely execute API operations through a secure, policy-controlled interface. This guide explains the features and how to use them.

## What is MCP?

MCP is a standard protocol that allows AI agents (like Claude, GPT, etc.) to interact with external tools and APIs in a controlled manner. MicroRapid implements an MCP server that acts as a secure gateway between AI agents and your APIs.

## Key Features for Agents

### 1. **Safe API Execution**
- Agents can execute API operations without direct access to credentials
- All operations go through policy enforcement
- Automatic validation of inputs and outputs

### 2. **Policy-Based Access Control**
- Define what operations agents can perform
- Set conditions based on method, path, time, etc.
- Deny access to sensitive endpoints
- Require authentication for specific operations

### 3. **Comprehensive Audit Logging**
- Every agent action is logged with unique IDs
- Track what operations were attempted/executed
- Monitor policy decisions (allow/deny)
- Automatic log rotation and compression

### 4. **Response Redaction**
- Automatic removal of sensitive data from responses
- Redacts: API keys, tokens, passwords, credit cards
- Configurable redaction patterns
- Agents never see actual secrets

### 5. **Auth Profile Management**
- Store credentials securely with environment variables
- Agents reference profiles by name, not actual credentials
- Support for Bearer, Basic, and API Key authentication

## How to Use MCP Integration

### Step 1: Install and Build

```bash
# Clone the repository
git clone https://github.com/deepwissen/api-runtime.git
cd api-runtime

# Build the MCP server
cd agent
cargo build --release
```

### Step 2: Configure the Server

Create a configuration file `mcp-server.toml`:

```toml
[server]
host = "127.0.0.1"
port = 8080

[defaults]
spec_path = ".mrapids/api.yaml"
policy_file = ".mrapids/policy.yaml"
env = "production"
auth_profile = "default"

[security]
allow_override_env = false
allow_override_auth = false
redact_patterns = ["password", "secret", "token", "key"]

[audit]
enabled = true
directory = ".mrapids/audit"
level = "detailed"
rotation_size_mb = 100
rotation_time = "daily"
max_files = 30
compress = true
```

### Step 3: Define Access Policies

Create `.mrapids/policy.yaml`:

```yaml
version: "1.0"
metadata:
  name: "agent-policy"
  description: "Policy for AI agent access"

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

rules:
  # Allow read operations on all endpoints
  - name: "allow-reads"
    pattern: "*"
    allow:
      methods: ["GET"]
      operations: ["list*", "get*", "search*"]
    
  # Deny access to admin endpoints
  - name: "deny-admin"
    pattern: "*/admin/*"
    deny:
      reason: "Admin endpoints are restricted"
  
  # Allow specific write operations
  - name: "allow-safe-writes"
    pattern: "*/comments"
    allow:
      methods: ["POST"]
      conditions:
        - type: "time_window"
          start: "09:00"
          end: "17:00"
```

### Step 4: Set Up Authentication

Create auth profiles in `.mrapids/auth/`:

```toml
# .mrapids/auth/github.toml
[profile]
name = "github"
type = "bearer"
token_env = "GITHUB_TOKEN"
```

```toml
# .mrapids/auth/api-server.toml
[profile]
name = "api-server"
type = "api_key"
header = "X-API-Key"
key_env = "API_SERVER_KEY"
```

Set environment variables:
```bash
export GITHUB_TOKEN="your-github-token"
export API_SERVER_KEY="your-api-key"
```

### Step 5: Add Your API Specification

Place your OpenAPI spec in `.mrapids/api.yaml`:

```yaml
openapi: "3.0.0"
info:
  title: "My API"
  version: "1.0.0"
servers:
  - url: https://api.example.com
paths:
  /users:
    get:
      operationId: listUsers
      summary: List all users
  /users/{id}:
    get:
      operationId: getUser
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
```

### Step 6: Start the MCP Server

```bash
./target/release/mrapids-agent --config mcp-server.toml --config-dir .mrapids
```

Server will start on `http://localhost:8080`

### Step 7: Use with AI Agents

#### Available Tools

The MCP server exposes three main tools via JSON-RPC:

1. **List Operations**
```json
{
  "jsonrpc": "2.0",
  "method": "tools/list",
  "params": {
    "filter": {
      "method": "GET",
      "tag": "users",
      "pattern": "search"
    }
  },
  "id": 1
}
```

2. **Show Operation Details**
```json
{
  "jsonrpc": "2.0",
  "method": "tools/show",
  "params": {
    "operation_id": "getUser"
  },
  "id": 2
}
```

3. **Run Operation**
```json
{
  "jsonrpc": "2.0",
  "method": "tools/run",
  "params": {
    "operation_id": "getUser",
    "parameters": {
      "id": "123"
    },
    "auth_profile": "github"
  },
  "id": 3
}
```

## Example: AI Agent Workflow

### 1. Agent Discovers Available Operations

```python
# Agent asks: "What user operations are available?"
response = mcp_client.call("tools/list", {
    "filter": {"pattern": "user"}
})
# Returns: listUsers, getUser, createUser, updateUser, deleteUser
```

### 2. Agent Gets Operation Details

```python
# Agent asks: "How do I get a specific user?"
response = mcp_client.call("tools/show", {
    "operation_id": "getUser"
})
# Returns: Parameters needed, response schema, etc.
```

### 3. Agent Executes Operation

```python
# Agent executes: "Get user with ID 123"
response = mcp_client.call("tools/run", {
    "operation_id": "getUser",
    "parameters": {"id": "123"}
})
# Returns: User data (with sensitive fields redacted)
```

## Security Features in Action

### Policy Enforcement Example

If an agent tries to access a restricted endpoint:

```python
# Agent tries: "Delete all users"
response = mcp_client.call("tools/run", {
    "operation_id": "deleteAllUsers"
})

# Response:
{
  "error": {
    "code": 1001,
    "message": "Admin endpoints are restricted",
    "data": {
      "rule": "deny-admin",
      "operation": "deleteAllUsers"
    }
  }
}
```

### Response Redaction Example

When sensitive data is returned:

```python
# Original API response:
{
  "user": {
    "id": "123",
    "name": "John Doe",
    "api_key": "sk-1234567890abcdef",
    "password_hash": "$2b$10$...",
    "email": "john@example.com"
  }
}

# Redacted response to agent:
{
  "user": {
    "id": "123",
    "name": "John Doe",
    "api_key": "[REDACTED:API_KEY]",
    "password_hash": "[REDACTED:PASSWORD]",
    "email": "john@example.com"
  }
}
```

### Audit Log Example

Every operation creates an audit entry:

```json
{
  "id": "550e8400-e29b-41d4-a716-446655440000",
  "timestamp": "2024-01-20T10:30:00Z",
  "agent_id": "mcp-agent",
  "operation": {
    "operation_id": "getUser",
    "method": "GET",
    "url": "https://api.example.com/users/123",
    "auth_profile": "github",
    "parameters": {"id": "123"}
  },
  "policy": {
    "decision": "allow",
    "rule": "allow-reads"
  },
  "response": {
    "status": "success",
    "status_code": 200,
    "duration_ms": 145
  }
}
```

## Integration with AI Frameworks

### Claude Desktop (via MCP)

1. Add to Claude's config:
```json
{
  "mcpServers": {
    "mrapids": {
      "command": "mrapids-agent",
      "args": ["--config", "mcp-server.toml"]
    }
  }
}
```

2. Claude can then use commands like:
- "List all available API operations"
- "Show me how to create a user"
- "Get the user with ID 123"

### Custom AI Integration

```python
import openai
import requests

class MRapidsAgent:
    def __init__(self, mcp_url="http://localhost:8080"):
        self.mcp_url = mcp_url
        
    def execute_operation(self, operation_id, params=None):
        response = requests.post(self.mcp_url, json={
            "jsonrpc": "2.0",
            "method": "tools/run",
            "params": {
                "operation_id": operation_id,
                "parameters": params or {}
            },
            "id": 1
        })
        return response.json()
    
# Use with OpenAI
agent = MRapidsAgent()
result = agent.execute_operation("getUser", {"id": "123"})
```

## Best Practices

### 1. **Principle of Least Privilege**
- Only allow operations that agents actually need
- Use specific patterns, not wildcards
- Deny by default, allow by exception

### 2. **Monitor Audit Logs**
- Regularly review agent activities
- Set up alerts for denied operations
- Track usage patterns

### 3. **Test Policies**
- Use the policy test framework
- Verify both allows and denies work correctly
- Test edge cases and time conditions

### 4. **Secure Credentials**
- Never expose actual credentials to agents
- Use environment variables for secrets
- Rotate credentials regularly

### 5. **Response Safety**
- Configure redaction patterns for your API
- Test that sensitive data is properly redacted
- Consider what agents should/shouldn't see

## Troubleshooting

### Agent Can't Connect
```bash
# Check server is running
curl http://localhost:8080 -d '{"jsonrpc":"2.0","method":"health","id":1}'

# Check logs
tail -f .mrapids/audit/mcp-audit-current.log
```

### Operation Denied
- Check audit logs for the denial reason
- Review policy rules with `mrapids validate-policy`
- Ensure time conditions are met

### Missing Auth
- Verify environment variables are set
- Check auth profile exists in `.mrapids/auth/`
- Ensure profile name matches in request

## Summary

MicroRapid's MCP integration provides:
- **Safe API access** for AI agents
- **Policy-based control** over operations
- **Complete audit trail** of all activities
- **Automatic security** through redaction
- **Simple integration** via JSON-RPC

This enables AI agents to interact with your APIs while maintaining security, compliance, and control over what they can access and execute.