This repository was archived by the owner on Mar 7, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.py
More file actions
360 lines (288 loc) · 12.8 KB
/
Copy pathmain.py
File metadata and controls
360 lines (288 loc) · 12.8 KB
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
"""
MCP Tool Server with AgentMesh Governance
This example demonstrates how to build a Model Context Protocol (MCP) server
secured with AgentMesh identity, policies, and audit logging.
MCP is Anthropic's protocol for connecting AI models to tools and data sources.
"""
import asyncio
import json
from datetime import datetime
from pathlib import Path
from typing import Any, Dict
from agentmesh import (
AgentIdentity,
PolicyEngine,
AuditLog,
RewardEngine,
CapabilityScope,
)
# Simulated MCP server framework (in production, use actual MCP SDK)
class MCPServer:
"""Minimal MCP server implementation for demonstration."""
def __init__(self, name: str):
self.name = name
self.tools: Dict[str, Any] = {}
def tool(self, name: str):
"""Decorator to register a tool."""
def decorator(func):
self.tools[name] = func
return func
return decorator
async def handle_request(self, tool_name: str, params: Dict[str, Any]) -> Dict[str, Any]:
"""Handle an MCP tool invocation."""
if tool_name not in self.tools:
return {"error": f"Tool {tool_name} not found"}
try:
result = await self.tools[tool_name](params)
return {"result": result}
except Exception as e:
return {"error": str(e)}
class GovernedMCPServer:
"""MCP server wrapped with AgentMesh governance."""
def __init__(self, config_path: str = "agentmesh.yaml"):
"""Initialize governed MCP server."""
self.config_path = config_path
# Create AgentMesh identity
print("🔐 Creating AgentMesh identity...")
self.identity = AgentIdentity.create(
name="mcp-tool-server",
sponsor="devops@company.com",
capabilities=["tool:filesystem:read", "tool:database:query", "tool:api:call"]
)
print(f"✓ Identity created: {self.identity.did}")
print(f"✓ Public key: {self.identity.public_key[:32]}...")
# Load policies
print("\n📋 Loading governance policies...")
self.policy_engine = PolicyEngine()
self._load_policies()
# Initialize audit log
print("\n📝 Initializing audit log...")
self.audit_log = AuditLog(
agent_id=self.identity.did,
)
# Initialize reward engine (trust scoring)
print("\n⭐ Initializing reward engine...")
self.reward_engine = RewardEngine()
self.trust_score = 800 # Starting score
# Create MCP server
self.mcp_server = MCPServer("agentmesh-example")
self._register_tools()
print(f"\n✓ MCP server initialized with trust score: {self.trust_score}/1000")
def _load_policies(self):
"""Load policy files."""
policies_dir = Path(__file__).parent / "policies"
for policy_file in policies_dir.glob("*.yaml"):
try:
# In real implementation, PolicyEngine.load_from_file()
print(f" ✓ Loaded policy: {policy_file.name}")
except Exception as e:
print(f" ✗ Failed to load {policy_file.name}: {e}")
def _register_tools(self):
"""Register MCP tools with governance."""
@self.mcp_server.tool("filesystem_read")
async def filesystem_read(params: Dict[str, Any]) -> str:
"""Read a file from the filesystem (with governance)."""
path = params.get("path")
# Policy check
policy_result = self._check_policy(
action="filesystem_read",
params={"path": path}
)
if not policy_result["allowed"]:
raise PermissionError(f"Policy violation: {policy_result['reason']}")
# Simulate file read (in production, actually read file)
content = f"[Simulated content of {path}]"
# Log to audit trail
self._audit_log_event("filesystem_read", params, "success")
# Update trust score
self._update_trust_score("filesystem_read", success=True)
return content
@self.mcp_server.tool("database_query")
async def database_query(params: Dict[str, Any]) -> list:
"""Query a database (with governance)."""
query = params.get("query")
# Policy check
policy_result = self._check_policy(
action="database_query",
params={"query": query}
)
if not policy_result["allowed"]:
raise PermissionError(f"Policy violation: {policy_result['reason']}")
# Simulate database query
results = [
{"id": 1, "name": "Alice", "email": "alice@example.com"},
{"id": 2, "name": "Bob", "email": "bob@example.com"},
]
# Check for PII in output (output sanitization)
sanitized_results = self._sanitize_output(results)
# Log to audit trail
self._audit_log_event("database_query", params, "success")
# Update trust score
self._update_trust_score("database_query", success=True)
return sanitized_results
@self.mcp_server.tool("api_call")
async def api_call(params: Dict[str, Any]) -> Dict[str, Any]:
"""Call an external API (with rate limiting)."""
endpoint = params.get("endpoint")
method = params.get("method", "GET")
# Policy check (includes rate limiting)
policy_result = self._check_policy(
action="api_call",
params={"endpoint": endpoint, "method": method}
)
if not policy_result["allowed"]:
raise PermissionError(f"Policy violation: {policy_result['reason']}")
# Simulate API call
response = {
"status": 200,
"data": {"message": f"Response from {endpoint}"}
}
# Log to audit trail
self._audit_log_event("api_call", params, "success")
# Update trust score
self._update_trust_score("api_call", success=True)
return response
def _check_policy(self, action: str, params: Dict[str, Any]) -> Dict[str, bool]:
"""Check if action is allowed by policies."""
# Simulated policy engine (in production, use PolicyEngine.check())
# Example policies:
# 1. Rate limiting
if action == "api_call":
# In production, track actual rate limits
pass
# 2. Path restrictions for filesystem
if action == "filesystem_read":
path = params.get("path", "")
# Block access to sensitive paths
sensitive_paths = ["/etc/passwd", "/etc/shadow", "/.ssh"]
if any(path.startswith(p) for p in sensitive_paths):
return {
"allowed": False,
"reason": f"Access to {path} is blocked by policy"
}
# 3. SQL injection prevention
if action == "database_query":
query = params.get("query", "")
# Comprehensive dangerous SQL keywords (case-insensitive)
dangerous_keywords = [
"DROP", "DELETE", "TRUNCATE", "ALTER",
"UPDATE", "INSERT", "EXEC", "EXECUTE",
"CREATE", "GRANT", "REVOKE"
]
query_upper = query.upper()
if any(keyword in query_upper for keyword in dangerous_keywords):
return {
"allowed": False,
"reason": f"Dangerous SQL keywords detected in query"
}
return {"allowed": True, "reason": None}
def _sanitize_output(self, data: Any) -> Any:
"""Sanitize output to remove sensitive data."""
# Simulated output sanitization
# In production, use PolicyEngine output filters
if isinstance(data, list):
return [self._sanitize_output(item) for item in data]
if isinstance(data, dict):
sanitized = {}
for key, value in data.items():
# Redact fields that might contain PII
if key.lower() in ["password", "api_key", "secret", "token"]:
sanitized[key] = "[REDACTED]"
else:
sanitized[key] = value
return sanitized
return data
def _audit_log_event(self, action: str, params: Dict[str, Any], result: str):
"""Log event to audit trail."""
entry = {
"timestamp": datetime.utcnow().isoformat() + "Z",
"agent": self.identity.did,
"action": action,
"params": params,
"result": result,
"trust_score": self.trust_score
}
# In production, use AuditLog.log()
print(f"\n📋 Audit: {action} - {result} (trust score: {self.trust_score})")
def _update_trust_score(self, action: str, success: bool):
"""Update trust score based on action."""
# Simulated reward engine
# In production, use RewardEngine.update_score()
if success:
# Small increase for successful actions
self.trust_score = min(1000, self.trust_score + 1)
else:
# Larger decrease for failures
self.trust_score = max(0, self.trust_score - 10)
# If trust score drops too low, revoke credentials
if self.trust_score < 500:
print(f"\n⚠️ WARNING: Trust score dropped to {self.trust_score}. Credentials may be revoked.")
async def run(self):
"""Run the MCP server."""
print("\n" + "="*70)
print("🚀 MCP Tool Server with AgentMesh Governance")
print("="*70)
print(f"\nAgent DID: {self.identity.did}")
print(f"Trust Score: {self.trust_score}/1000")
print(f"\nAvailable tools:")
for tool_name in self.mcp_server.tools.keys():
print(f" • {tool_name}")
print("\n" + "="*70)
# Demo: Simulate some tool invocations
await self.demo_tool_invocations()
async def demo_tool_invocations(self):
"""Demo tool invocations to show governance in action."""
print("\n🧪 Demo: Testing tool invocations with governance\n")
# Test 1: Allowed filesystem read
print("\n1️⃣ Test: Reading allowed file")
result = await self.mcp_server.handle_request(
"filesystem_read",
{"path": "/data/users.json"}
)
print(f" Result: {result}")
# Test 2: Blocked filesystem read
print("\n2️⃣ Test: Reading blocked file")
result = await self.mcp_server.handle_request(
"filesystem_read",
{"path": "/etc/passwd"}
)
print(f" Result: {result}")
# Test 3: Database query
print("\n3️⃣ Test: Database query")
result = await self.mcp_server.handle_request(
"database_query",
{"query": "SELECT * FROM users"}
)
print(f" Result: {result}")
# Test 4: Blocked dangerous query
print("\n4️⃣ Test: Dangerous SQL query (blocked)")
result = await self.mcp_server.handle_request(
"database_query",
{"query": "DROP TABLE users"}
)
print(f" Result: {result}")
# Test 5: API call
print("\n5️⃣ Test: API call")
result = await self.mcp_server.handle_request(
"api_call",
{"endpoint": "https://api.weather.com/current", "method": "GET"}
)
print(f" Result: {result}")
print("\n" + "="*70)
print(f"✓ Demo complete. Final trust score: {self.trust_score}/1000")
print("="*70)
async def main():
"""Main entry point."""
# Initialize governed MCP server
server = GovernedMCPServer()
# Run the server
await server.run()
print("\n💡 In production, this MCP server would:")
print(" • Connect to Claude Desktop or other MCP clients")
print(" • Enforce real policies from YAML files")
print(" • Write to persistent audit logs")
print(" • Integrate with SPIFFE for mTLS")
print(" • Report compliance metrics to dashboards")
print("\n🔗 Learn more: https://github.com/imran-siddique/agent-mesh")
if __name__ == "__main__":
asyncio.run(main())