UNPKG

atp-sdk

Version:

Official TypeScript SDK for Agent Trust Protocolâ„¢ - Build secure, verifiable, and trustworthy applications with decentralized identity, verifiable credentials, payment protocols (AP2/ACP), and robust access control

165 lines • 5.37 kB
/** * Model Context Protocol (MCP) Adapter * * Implements ATP security and monitoring for Model Context Protocol agents */ import { BaseProtocolAdapter } from '../base/adapter.js'; import { Protocol } from '../base/types.js'; /** * MCP Protocol Adapter */ export class MCPAdapter extends BaseProtocolAdapter { constructor() { super(); this.eventStreams = new Map(); } identify() { return { protocol: Protocol.MCP, version: '1.0.0', name: 'Model Context Protocol', description: 'Anthropic Model Context Protocol with ATP security', capabilities: [ 'context-management', 'tool-protocols', 'retrieval-patterns', 'app-integrations' ], metadata: { vendor: 'Anthropic', documentation: 'https://modelcontextprotocol.io' } }; } monitor(agent) { const agentDid = agent.did; // Check if already monitoring if (this.eventStreams.has(agentDid)) { return this.eventStreams.get(agentDid); } // Create new event stream const stream = this.createEventStream(agent); this.eventStreams.set(agentDid, stream); return stream; } /** * Create event stream for MCP agent */ createEventStream(agent) { const self = this; return { subscribe(observer) { let active = true; // Simulate MCP event monitoring // In real implementation, this would hook into actual MCP events const intervalId = setInterval(() => { if (!active) { clearInterval(intervalId); return; } // Generate sample events (in real implementation, capture actual events) const event = { id: self.generateEventId(), protocol: Protocol.MCP, timestamp: new Date().toISOString(), type: 'mcp.context.update', source: { agentDid: agent.did, agentName: agent.name }, data: { contextId: 'ctx_' + Date.now(), updates: {}, timestamp: new Date().toISOString() } }; observer.next(event); }, 5000); // Poll every 5 seconds return { unsubscribe() { active = false; clearInterval(intervalId); if (observer.complete) { observer.complete(); } } }; }, pipe(...operations) { let result = this; for (const operation of operations) { result = operation(result); } return result; } }; } /** * Generate unique event ID */ generateEventId() { return `mcp_event_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`; } /** * Monitor context updates */ async monitorContextUpdates(agent) { const baseStream = this.monitor(agent); const observable = { subscribe(observer) { return baseStream.subscribe({ next: (event) => { if (event.type === 'mcp.context.update') { observer.next(event.data); } }, error: observer.error, complete: observer.complete }); }, pipe(...operations) { let result = observable; for (const operation of operations) { result = operation(result); } return result; } }; return observable; } /** * Monitor tool invocations */ async monitorToolInvocations(agent) { const baseStream = this.monitor(agent); const observable = { subscribe(observer) { return baseStream.subscribe({ next: (event) => { if (event.type === 'mcp.tool.invoke') { observer.next(event.data); } }, error: observer.error, complete: observer.complete }); }, pipe(...operations) { let result = observable; for (const operation of operations) { result = operation(result); } return result; } }; return observable; } /** * Cleanup MCP adapter resources */ async cleanup() { this.eventStreams.clear(); await super.cleanup(); } } //# sourceMappingURL=mcp-adapter.js.map