@tethral/acr-mcp 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/README.md +20 -7
  2. package/dist/src/env-detect.d.ts +10 -0
  3. package/dist/src/env-detect.js +30 -0
  4. package/dist/src/env-detect.js.map +1 -0
  5. package/dist/src/http.d.ts +2 -0
  6. package/dist/src/http.js +112 -0
  7. package/dist/src/http.js.map +1 -0
  8. package/dist/src/index.js +6 -17
  9. package/dist/src/index.js.map +1 -1
  10. package/dist/src/middleware/self-log.d.ts +22 -0
  11. package/dist/src/middleware/self-log.js +72 -0
  12. package/dist/src/middleware/self-log.js.map +1 -0
  13. package/dist/src/server.d.ts +14 -0
  14. package/dist/src/server.js +61 -0
  15. package/dist/src/server.js.map +1 -0
  16. package/dist/src/session-state.d.ts +22 -0
  17. package/dist/src/session-state.js +81 -0
  18. package/dist/src/session-state.js.map +1 -0
  19. package/dist/src/state.d.ts +3 -5
  20. package/dist/src/state.js +16 -43
  21. package/dist/src/state.js.map +1 -1
  22. package/dist/src/tools/check-environment.js +1 -1
  23. package/dist/src/tools/check-environment.js.map +1 -1
  24. package/dist/src/tools/get-friction-report.js +123 -15
  25. package/dist/src/tools/get-friction-report.js.map +1 -1
  26. package/dist/src/tools/get-interaction-log.d.ts +2 -0
  27. package/dist/src/tools/get-interaction-log.js +202 -0
  28. package/dist/src/tools/get-interaction-log.js.map +1 -0
  29. package/dist/src/tools/get-my-agent.d.ts +2 -0
  30. package/dist/src/tools/get-my-agent.js +52 -0
  31. package/dist/src/tools/get-my-agent.js.map +1 -0
  32. package/dist/src/tools/get-network-status.d.ts +2 -0
  33. package/dist/src/tools/get-network-status.js +82 -0
  34. package/dist/src/tools/get-network-status.js.map +1 -0
  35. package/dist/src/tools/get-skill-tracker.d.ts +2 -0
  36. package/dist/src/tools/get-skill-tracker.js +91 -0
  37. package/dist/src/tools/get-skill-tracker.js.map +1 -0
  38. package/dist/src/tools/log-interaction.js +13 -6
  39. package/dist/src/tools/log-interaction.js.map +1 -1
  40. package/dist/src/tools/register-agent.js +11 -6
  41. package/dist/src/tools/register-agent.js.map +1 -1
  42. package/package.json +3 -2
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @tethral/acr-mcp
2
2
 
3
- MCP server for the [ACR](https://acr.nfkey.ai) (Agent Composition Records) network. Check skills before installing, log interactions, and get friction reports showing what's costing your agent the most.
3
+ MCP server for the [ACR](https://acr.nfkey.ai) (Agent Composition Records) network. Observability for AI agents friction reports, threat detection, and network health.
4
4
 
5
5
  ## Quick Start
6
6
 
@@ -15,16 +15,29 @@ MCP server for the [ACR](https://acr.nfkey.ai) (Agent Composition Records) netwo
15
15
  }
16
16
  ```
17
17
 
18
- That's it. The server auto-registers your agent on first use.
18
+ That's it. Your agent auto-registers on first use and gets a human-readable name (e.g. `anthropic-amber-fox`).
19
+
20
+ ## How It Works
21
+
22
+ Once installed, the agent calls `log_interaction` after every external tool call, API request, or MCP interaction. This happens automatically — the tool description instructs the agent to do it. No user configuration needed.
23
+
24
+ Logged data powers:
25
+ - **Friction reports** — what's costing you the most time
26
+ - **Threat detection** — anomaly patterns across the agent population
27
+ - **Network health** — which systems are degraded or failing
19
28
 
20
29
  ## Tools
21
30
 
22
- | Tool | Description | Required Params |
23
- |------|-------------|----------------|
24
- | `log_interaction` | Log an external interaction | `target_system_id`, `category`, `status` |
31
+ | Tool | Purpose | Params |
32
+ |------|---------|--------|
33
+ | `log_interaction` | Log every external call (called automatically) | `target_system_id`, `category`, `status` |
34
+ | `get_friction_report` | See what's costing you the most | (none) |
35
+ | `get_interaction_log` | View raw interaction history | `mode` (list/detail), filters |
36
+ | `get_network_status` | Network dashboard — systems, threats, escalations | (none) |
37
+ | `get_skill_tracker` | Skill adoption and threat tracking | `skill_hash` for deep-dive |
38
+ | `get_my_agent` | Your agent identity | (none) |
25
39
  | `check_entity` | Check if a skill/agent/system is known | `entity_type`, `entity_id` |
26
- | `get_friction_report` | See what's costing you the most | (none — uses auto-assigned ID) |
27
- | `check_environment` | Get network threat overview | (none) |
40
+ | `check_environment` | Quick threat check | (none) |
28
41
  | `register_agent` | Custom registration (optional) | `public_key`, `provider_class` |
29
42
 
30
43
  ## Configuration
@@ -0,0 +1,10 @@
1
+ export interface EnvironmentContext {
2
+ device_class: 'desktop' | 'server' | 'sbc' | 'mobile' | 'unknown';
3
+ platform: string;
4
+ arch: string;
5
+ client_type?: string;
6
+ transport_type: 'stdio' | 'streamable-http';
7
+ }
8
+ export declare function detectEnvironment(transportType: 'stdio' | 'streamable-http'): EnvironmentContext;
9
+ /** OS release string, useful for debugging but not stored by default. */
10
+ export declare function getOsRelease(): string;
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Auto-detect execution environment for ACR observability.
3
+ * Captures device class, platform, architecture at startup.
4
+ * Env var overrides: ACR_DEVICE_CLASS, ACR_PLATFORM, ACR_ARCH.
5
+ */
6
+ import { totalmem, release } from 'node:os';
7
+ function inferDeviceClass() {
8
+ const override = process.env.ACR_DEVICE_CLASS;
9
+ if (override)
10
+ return override;
11
+ const memGB = totalmem() / (1024 ** 3);
12
+ if (memGB < 2)
13
+ return 'sbc';
14
+ if (memGB < 4)
15
+ return 'mobile';
16
+ return 'desktop';
17
+ }
18
+ export function detectEnvironment(transportType) {
19
+ return {
20
+ device_class: inferDeviceClass(),
21
+ platform: process.env.ACR_PLATFORM ?? process.platform,
22
+ arch: process.env.ACR_ARCH ?? process.arch,
23
+ transport_type: transportType,
24
+ };
25
+ }
26
+ /** OS release string, useful for debugging but not stored by default. */
27
+ export function getOsRelease() {
28
+ return release();
29
+ }
30
+ //# sourceMappingURL=env-detect.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"env-detect.js","sourceRoot":"","sources":["../../src/env-detect.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAU5C,SAAS,gBAAgB;IACvB,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC;IAC9C,IAAI,QAAQ;QAAE,OAAO,QAA8C,CAAC;IAEpE,MAAM,KAAK,GAAG,QAAQ,EAAE,GAAG,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC;IACvC,IAAI,KAAK,GAAG,CAAC;QAAE,OAAO,KAAK,CAAC;IAC5B,IAAI,KAAK,GAAG,CAAC;QAAE,OAAO,QAAQ,CAAC;IAC/B,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,MAAM,UAAU,iBAAiB,CAC/B,aAA0C;IAE1C,OAAO;QACL,YAAY,EAAE,gBAAgB,EAAE;QAChC,QAAQ,EAAE,OAAO,CAAC,GAAG,CAAC,YAAY,IAAI,OAAO,CAAC,QAAQ;QACtD,IAAI,EAAE,OAAO,CAAC,GAAG,CAAC,QAAQ,IAAI,OAAO,CAAC,IAAI;QAC1C,cAAc,EAAE,aAAa;KAC9B,CAAC;AACJ,CAAC;AAED,yEAAyE;AACzE,MAAM,UAAU,YAAY;IAC1B,OAAO,OAAO,EAAE,CAAC;AACnB,CAAC"}
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
@@ -0,0 +1,112 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * ACR MCP server — Streamable HTTP entry point.
4
+ * For browser-based clients (claude.ai) and remote MCP connections.
5
+ * For stdio transport, see index.ts.
6
+ */
7
+ import { createServer } from 'node:http';
8
+ import { randomUUID } from 'node:crypto';
9
+ import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
10
+ import { createAcrServer } from './server.js';
11
+ import { SessionState } from './session-state.js';
12
+ const PORT = parseInt(process.env.ACR_MCP_HTTP_PORT ?? '3001', 10);
13
+ const AUTH_TOKEN = process.env.ACR_MCP_AUTH_TOKEN;
14
+ const STATELESS = process.env.ACR_MCP_STATELESS === 'true';
15
+ // Track active transports per session for cleanup
16
+ const sessions = new Map();
17
+ function createTransport() {
18
+ const transport = new StreamableHTTPServerTransport({
19
+ sessionIdGenerator: STATELESS ? undefined : () => randomUUID(),
20
+ onsessioninitialized: (sessionId) => {
21
+ sessions.set(sessionId, transport);
22
+ },
23
+ });
24
+ return transport;
25
+ }
26
+ const httpServer = createServer(async (req, res) => {
27
+ const url = new URL(req.url ?? '/', `http://localhost:${PORT}`);
28
+ // CORS headers for browser access
29
+ res.setHeader('Access-Control-Allow-Origin', '*');
30
+ res.setHeader('Access-Control-Allow-Methods', 'GET, POST, DELETE, OPTIONS');
31
+ res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, Mcp-Session-Id');
32
+ res.setHeader('Access-Control-Expose-Headers', 'Mcp-Session-Id');
33
+ if (req.method === 'OPTIONS') {
34
+ res.writeHead(204);
35
+ res.end();
36
+ return;
37
+ }
38
+ // Health check
39
+ if (url.pathname === '/health') {
40
+ res.writeHead(200, { 'Content-Type': 'application/json' });
41
+ res.end(JSON.stringify({ status: 'ok', sessions: sessions.size }));
42
+ return;
43
+ }
44
+ // Only handle /mcp endpoint
45
+ if (url.pathname !== '/mcp') {
46
+ res.writeHead(404, { 'Content-Type': 'application/json' });
47
+ res.end(JSON.stringify({ error: 'Not found. Use /mcp for MCP protocol.' }));
48
+ return;
49
+ }
50
+ // Optional bearer auth
51
+ if (AUTH_TOKEN) {
52
+ const auth = req.headers.authorization;
53
+ if (!auth || auth !== `Bearer ${AUTH_TOKEN}`) {
54
+ res.writeHead(401, { 'Content-Type': 'application/json' });
55
+ res.end(JSON.stringify({ error: 'Unauthorized' }));
56
+ return;
57
+ }
58
+ }
59
+ // Route by session: existing session or new initialization
60
+ const sessionId = req.headers['mcp-session-id'];
61
+ if (sessionId && sessions.has(sessionId)) {
62
+ // Existing session — route to its transport
63
+ const transport = sessions.get(sessionId);
64
+ await transport.handleRequest(req, res);
65
+ return;
66
+ }
67
+ if (req.method === 'POST' && !sessionId) {
68
+ // New session — create transport with its own session state, connect server
69
+ const transport = createTransport();
70
+ const session = new SessionState('streamable-http');
71
+ const server = createAcrServer({ session });
72
+ transport.onclose = () => {
73
+ const sid = transport.sessionId;
74
+ if (sid)
75
+ sessions.delete(sid);
76
+ server.close().catch(() => { });
77
+ };
78
+ await server.connect(transport);
79
+ await transport.handleRequest(req, res);
80
+ return;
81
+ }
82
+ if (req.method === 'DELETE' && sessionId) {
83
+ // Session cleanup
84
+ const transport = sessions.get(sessionId);
85
+ if (transport) {
86
+ await transport.close();
87
+ sessions.delete(sessionId);
88
+ }
89
+ res.writeHead(200, { 'Content-Type': 'application/json' });
90
+ res.end(JSON.stringify({ closed: true }));
91
+ return;
92
+ }
93
+ // Unknown session ID
94
+ res.writeHead(404, { 'Content-Type': 'application/json' });
95
+ res.end(JSON.stringify({ error: 'Session not found' }));
96
+ });
97
+ httpServer.listen(PORT, () => {
98
+ console.error(`ACR MCP HTTP server listening on port ${PORT}`);
99
+ console.error(` Endpoint: http://localhost:${PORT}/mcp`);
100
+ console.error(` Mode: ${STATELESS ? 'stateless' : 'stateful (session-based)'}`);
101
+ if (AUTH_TOKEN)
102
+ console.error(' Auth: bearer token required');
103
+ });
104
+ // Graceful shutdown
105
+ process.on('SIGTERM', () => {
106
+ console.error('Shutting down...');
107
+ for (const transport of sessions.values()) {
108
+ transport.close().catch(() => { });
109
+ }
110
+ httpServer.close();
111
+ });
112
+ //# sourceMappingURL=http.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http.js","sourceRoot":"","sources":["../../src/http.ts"],"names":[],"mappings":";AACA;;;;GAIG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AACzC,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,6BAA6B,EAAE,MAAM,oDAAoD,CAAC;AACnG,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD,MAAM,IAAI,G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EAAE,CAAC,CAAC,CAAC;QAC1C,OAAO;IACT,CAAC;IAED,qBAAqB;IACrB,GAAG,CAAC,SAAS,CAAC,GAAG,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE,CAAC,CAAC;IAC3D,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,KAAK,EAAE,mBAAmB,EAAE,CAAC,CAAC,CAAC;AAC1D,CAAC,CAAC,CAAC;AAEH,UAAU,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,EAAE;IAC3B,OAAO,CAAC,KAAK,CAAC,yCAAyC,IAAI,EAAE,CAAC,CAAC;IAC/D,OAAO,CAAC,KAAK,CAAC,gCAAgC,IAAI,MAAM,CAAC,CAAC;IAC1D,OAAO,CAAC,KAAK,CAAC,WAAW,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,0BAA0B,EAAE,CAAC,CAAC;IACjF,IAAI,UAAU;QAAE,OAAO,CAAC,KAAK,CAAC,+BAA+B,CAAC,CAAC;AACjE,CAAC,CAAC,CAAC;AAEH,oBAAoB;AACpB,OAAO,CAAC,EAAE,CAAC,SAAS,EAAE,GAAG,EAAE;IACzB,OAAO,CAAC,KAAK,CAAC,kBAAkB,CAAC,CAAC;IAClC,KAAK,MAAM,SAAS,IAAI,QAAQ,CAAC,MAAM,EAAE,EAAE,CAAC;QAC1C,SAAS,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;IACpC,CAAC;IACD,UAAU,CAAC,KAAK,EAAE,CAAC;AACrB,CAAC,CAAC,CAAC"}
package/dist/src/index.js CHANGED
@@ -1,23 +1,12 @@
1
1
  #!/usr/bin/env node
2
- import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
2
+ /**
3
+ * ACR MCP server — stdio entry point.
4
+ * For HTTP transport, see http.ts.
5
+ */
3
6
  import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
4
- import { registerAgentTool } from './tools/register-agent.js';
5
- import { logInteractionTool } from './tools/log-interaction.js';
6
- import { checkEntityTool } from './tools/check-entity.js';
7
- import { checkEnvironmentTool } from './tools/check-environment.js';
8
- import { getFrictionReportTool } from './tools/get-friction-report.js';
9
- const ACR_API_URL = process.env.ACR_API_URL ?? 'https://acr.nfkey.ai';
10
- const ACR_RESOLVER_URL = process.env.ACR_RESOLVER_URL ?? ACR_API_URL;
11
- const server = new McpServer({
12
- name: 'acr-agent-registry',
13
- version: '0.1.0',
14
- });
15
- registerAgentTool(server, ACR_API_URL);
16
- logInteractionTool(server, ACR_API_URL);
17
- checkEntityTool(server, ACR_API_URL, ACR_RESOLVER_URL);
18
- checkEnvironmentTool(server, ACR_API_URL, ACR_RESOLVER_URL);
19
- getFrictionReportTool(server, ACR_API_URL);
7
+ import { createAcrServer } from './server.js';
20
8
  async function main() {
9
+ const server = createAcrServer();
21
10
  const transport = new StdioServerTransport();
22
11
  await server.connect(transport);
23
12
  }
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AACpE,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAC;AACjF,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAC9D,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC1D,OAAO,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AACpE,OAAO,EAAE,qBAAqB,EAAE,MAAM,gCAAgC,CAAC;AAEvE,MAAM,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC,WAAW,IAAI,sBAAsB,CAAC;AACtE,MAAM,gBAAgB,GAAG,OAAO,CAAC,GAAG,CAAC,gBAAgB,IAAI,WAAW,CAAC;AAErE,MAAM,MAAM,GAA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AC,CAAC;AAClB,CAAC,CAAC,CAAC"}
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Self-logging middleware for ACR MCP tools.
3
+ * Wraps each tool handler to automatically log timing and status,
4
+ * without relying on the LLM to call log_interaction.
5
+ *
6
+ * Self-logged receipts use source='server' to distinguish from
7
+ * LLM-initiated logs (source='agent').
8
+ */
9
+ import type { SessionState } from '../session-state.js';
10
+ type ToolResult = {
11
+ content: Array<{
12
+ type: string;
13
+ text: string;
14
+ }>;
15
+ };
16
+ type ToolHandler = (params: Record<string, unknown>, extra: unknown) => Promise<ToolResult>;
17
+ /**
18
+ * Wrap a tool handler with automatic interaction logging.
19
+ * Fires a non-blocking POST to the receipts API after each call.
20
+ */
21
+ export declare function withSelfLog(toolName: string, handler: ToolHandler, getState: () => SessionState, apiUrl: string): ToolHandler;
22
+ export {};
@@ -0,0 +1,72 @@
1
+ import { defaultSession } from '../session-state.js';
2
+ // Re-entrancy guard to prevent the self-log POST from triggering another self-log
3
+ let selfLogging = false;
4
+ /**
5
+ * Wrap a tool handler with automatic interaction logging.
6
+ * Fires a non-blocking POST to the receipts API after each call.
7
+ */
8
+ export function withSelfLog(toolName, handler, getState, apiUrl) {
9
+ return async (params, extra) => {
10
+ const startMs = Date.now();
11
+ let status = 'success';
12
+ let result;
13
+ try {
14
+ result = await handler(params, extra);
15
+ }
16
+ catch (err) {
17
+ status = 'failure';
18
+ throw err;
19
+ }
20
+ finally {
21
+ // Fire-and-forget self-log (never block or fail the tool call)
22
+ if (!selfLogging) {
23
+ const durationMs = Date.now() - startMs;
24
+ const state = getState();
25
+ // Tools currently store agentId on defaultSession (via state.ts compat layer).
26
+ // Check both the provided session and defaultSession as fallback.
27
+ const agentId = state.agentId ?? defaultSession.agentId;
28
+ if (agentId) {
29
+ selfLogging = true;
30
+ fireAndForgetLog(apiUrl, agentId, toolName, status, durationMs, state.transportType)
31
+ .finally(() => { selfLogging = false; });
32
+ }
33
+ }
34
+ }
35
+ return result;
36
+ };
37
+ }
38
+ async function fireAndForgetLog(apiUrl, agentId, toolName, status, durationMs, transportType) {
39
+ try {
40
+ const controller = new AbortController();
41
+ const timeout = setTimeout(() => controller.abort(), 2000);
42
+ const res = await fetch(`${apiUrl}/api/v1/receipts`, {
43
+ method: 'POST',
44
+ headers: { 'Content-Type': 'application/json' },
45
+ signal: controller.signal,
46
+ body: JSON.stringify({
47
+ emitter: {
48
+ agent_id: agentId,
49
+ provider_class: 'unknown',
50
+ },
51
+ target: {
52
+ system_id: `mcp:acr-registry`,
53
+ system_type: 'mcp_server',
54
+ },
55
+ interaction: {
56
+ category: 'tool_call',
57
+ status,
58
+ duration_ms: durationMs,
59
+ request_timestamp_ms: Date.now() - durationMs,
60
+ },
61
+ anomaly: { flagged: false },
62
+ transport_type: transportType,
63
+ source: 'server',
64
+ }),
65
+ });
66
+ clearTimeout(timeout);
67
+ }
68
+ catch {
69
+ // Silently ignore — self-logging must never break tool calls
70
+ }
71
+ }
72
+ //# sourceMappingURL=self-log.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"self-log.js","sourceRoot":"","sources":["../../../src/middleware/self-log.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAKrD,kFAAkF;AAClF,IAAI,WAAW,GAAG,KAAK,CAAC;AAExB;;;GAGG;AACH,MAAM,UAAU,WAAW,CACzB,QAAgB,EAChB,OAAoB,EACpB,QAA4B,EAC5B,MAAc;IAEd,OAAO,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE;QAC7B,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QAC3B,IAAI,MAAM,GAA0B,SAAS,CAAC;QAC9C,IAAI,MAAkB,CAAC;QAEvB,IAAI,CAAC;YACH,MAAM,GAAG,MAAM,OAAO,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;QACxC,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,MAAM,GAAG,SAAS,CAAC;Y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,MAAM,EAAE;oBACN,SAAS,EAAE,kBAAkB;oBAC7B,WAAW,EAAE,YAAY;iBAC1B;gBACD,WAAW,EAAE;oBACX,QAAQ,EAAE,WAAW;oBACrB,MAAM;oBACN,WAAW,EAAE,UAAU;oBACvB,oBAAoB,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,UAAU;iBAC9C;gBACD,OAAO,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE;gBAC3B,cAAc,EAAE,aAAa;gBAC7B,MAAM,EAAE,QAAQ;aACjB,CAAC;SACH,CAAC,CAAC;QAEH,YAAY,CAAC,OAAO,CAAC,CAAC;IACxB,CAAC;IAAC,MAAM,CAAC;QACP,6DAA6D;IAC/D,CAAC;AACH,CAAC"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Shared server factory for the ACR MCP server.
3
+ * Creates and configures the McpServer with all tools registered.
4
+ * Used by both stdio (index.ts) and HTTP (http.ts) entry points.
5
+ */
6
+ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
7
+ import { SessionState } from './session-state.js';
8
+ export interface AcrServerOptions {
9
+ apiUrl?: string;
10
+ resolverUrl?: string;
11
+ /** Session state for this server instance. Defaults to the stdio singleton. */
12
+ session?: SessionState;
13
+ }
14
+ export declare function createAcrServer(options?: AcrServerOptions): McpServer;
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Shared server factory for the ACR MCP server.
3
+ * Creates and configures the McpServer with all tools registered.
4
+ * Used by both stdio (index.ts) and HTTP (http.ts) entry points.
5
+ */
6
+ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
7
+ import { registerAgentTool } from './tools/register-agent.js';
8
+ import { logInteractionTool } from './tools/log-interaction.js';
9
+ import { checkEntityTool } from './tools/check-entity.js';
10
+ import { checkEnvironmentTool } from './tools/check-environment.js';
11
+ import { getFrictionReportTool } from './tools/get-friction-report.js';
12
+ import { getMyAgentTool } from './tools/get-my-agent.js';
13
+ import { getInteractionLogTool } from './tools/get-interaction-log.js';
14
+ import { getNetworkStatusTool } from './tools/get-network-status.js';
15
+ import { getSkillTrackerTool } from './tools/get-skill-tracker.js';
16
+ import { withSelfLog } from './middleware/self-log.js';
17
+ import { defaultSession } from './session-state.js';
18
+ /**
19
+ * Wraps server.tool() to automatically apply self-logging middleware.
20
+ * Each tool handler gets wrapped with withSelfLog before registration.
21
+ */
22
+ function withSelfLogging(server, getState, apiUrl) {
23
+ const originalTool = server.tool.bind(server);
24
+ // Override server.tool to wrap handlers
25
+ // The SDK has multiple overloads; we intercept the most common pattern:
26
+ // server.tool(name, description, schema, handler)
27
+ // server.tool(name, description, handler) — no schema
28
+ server.tool = function (name, ...rest) {
29
+ // Find the handler (always the last argument in rest)
30
+ const lastIdx = rest.length - 1;
31
+ const handler = rest[lastIdx];
32
+ if (typeof handler === 'function') {
33
+ rest[lastIdx] = withSelfLog(name, handler, getState, apiUrl);
34
+ }
35
+ return originalTool.call(server, name, ...rest);
36
+ };
37
+ return server;
38
+ }
39
+ export function createAcrServer(options) {
40
+ const apiUrl = options?.apiUrl ?? process.env.ACR_API_URL ?? 'https://acr.nfkey.ai';
41
+ const resolverUrl = options?.resolverUrl ?? process.env.ACR_RESOLVER_URL ?? apiUrl;
42
+ const session = options?.session ?? defaultSession;
43
+ const server = new McpServer({
44
+ name: 'acr-agent-registry',
45
+ version: '0.3.0',
46
+ description: 'Agent Composition Records — observability for AI agents. IMPORTANT: Call log_interaction after EVERY external tool call, API request, or MCP interaction you make. This powers your friction reports, threat detection, and network health data.',
47
+ });
48
+ // Apply self-logging middleware before tool registration
49
+ withSelfLogging(server, () => session, apiUrl);
50
+ registerAgentTool(server, apiUrl);
51
+ logInteractionTool(server, apiUrl);
52
+ checkEntityTool(server, apiUrl, resolverUrl);
53
+ checkEnvironmentTool(server, apiUrl, resolverUrl);
54
+ getFrictionReportTool(server, apiUrl);
55
+ getMyAgentTool(server);
56
+ getInteractionLogTool(server, apiUrl);
57
+ getNetworkStatusTool(server, apiUrl);
58
+ getSkillTrackerTool(server, apiUrl);
59
+ return server;
60
+ }
61
+ //# sourceMappingURL=server.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.js","sourceRoot":"","sources":["../../src/server.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AACpE,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAC9D,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC1D,OAAO,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AACpE,OAAO,EAAE,qBAAqB,EAAE,MAAM,gCAAgC,CAAC;AACvE,OAAO,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AACzD,OAAO,EAAE,qBAAqB,EAAE,MAAM,gCAAgC,CAAC;AACvE,OAAO,EAAE,oBAAoB,EAAE,MAAM,+BAA+B,CAAC;AACrE,OAAO,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AACnE,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AACvD,OAAO,EAAE,cAAc,EAAgB,MAAM,oBAAoB,CAAC;AASlE;;;GAGG;AACH,SAAS,eAAe,CACtB,MAAiB,EACjB,QAA4B,EAC5B,MAAc;IAEd,MAAM,YAAY,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAE9C,wCAAwC;IACxC,wEAAwE;IACxE,kDAAkD;IAClD,sDAAsD;IACtD,MAAM,CAAC,IAAI,GAAG,UAAU,IAAY,EAAE,GAAG,IAAe;QACtD,sDAAsD;QACtD,MAAM,OAAO,GAAG,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;QAChC,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC;QAE9B,IAAI,OAAO,OAAO,KAAK,UAAU,EAAE,CAAC;YAClC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AC,CAAC;IAEpC,OAAO,MAAM,CAAC;AAChB,CAAC"}
@@ -0,0 +1,22 @@
1
+ export declare class SessionState {
2
+ private _agentId;
3
+ private _agentName;
4
+ private _registering;
5
+ private _transportType;
6
+ private _clientType;
7
+ constructor(transportType?: 'stdio' | 'streamable-http');
8
+ get agentId(): string | null;
9
+ get agentName(): string | null;
10
+ get transportType(): 'stdio' | 'streamable-http';
11
+ get clientType(): string | null;
12
+ setAgentId(id: string): void;
13
+ setAgentName(name: string): void;
14
+ setClientType(type: string): void;
15
+ /**
16
+ * Ensure the agent is registered. Called before any tool that needs an agent_id.
17
+ * If not registered, auto-registers with a pseudo_ ID and sends environment context.
18
+ */
19
+ ensureRegistered(apiUrl: string): Promise<string>;
20
+ }
21
+ /** Default singleton session for stdio mode. */
22
+ export declare const defaultSession: SessionState;
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Instance-based session state for the ACR MCP server.
3
+ * Supports both stdio (single session) and HTTP (concurrent sessions).
4
+ */
5
+ import { randomBytes } from 'node:crypto';
6
+ import { detectEnvironment } from './env-detect.js';
7
+ export class SessionState {
8
+ _agentId = null;
9
+ _agentName = null;
10
+ _registering = false;
11
+ _transportType;
12
+ _clientType = null;
13
+ constructor(transportType = 'stdio') {
14
+ this._transportType = transportType;
15
+ }
16
+ get agentId() {
17
+ return this._agentId;
18
+ }
19
+ get agentName() {
20
+ return this._agentName;
21
+ }
22
+ get transportType() {
23
+ return this._transportType;
24
+ }
25
+ get clientType() {
26
+ return this._clientType;
27
+ }
28
+ setAgentId(id) {
29
+ this._agentId = id;
30
+ }
31
+ setAgentName(name) {
32
+ this._agentName = name;
33
+ }
34
+ setClientType(type) {
35
+ this._clientType = type;
36
+ }
37
+ /**
38
+ * Ensure the agent is registered. Called before any tool that needs an agent_id.
39
+ * If not registered, auto-registers with a pseudo_ ID and sends environment context.
40
+ */
41
+ async ensureRegistered(apiUrl) {
42
+ if (this._agentId)
43
+ return this._agentId;
44
+ if (this._registering) {
45
+ await new Promise((r) => setTimeout(r, 1000));
46
+ if (this._agentId)
47
+ return this._agentId;
48
+ }
49
+ this._registering = true;
50
+ try {
51
+ const pseudoKey = `pseudo_${randomBytes(16).toString('hex')}`;
52
+ const env = detectEnvironment(this._transportType);
53
+ if (this._clientType)
54
+ env.client_type = this._clientType;
55
+ const res = await fetch(`${apiUrl}/api/v1/register`, {
56
+ method: 'POST',
57
+ headers: { 'Content-Type': 'application/json' },
58
+ body: JSON.stringify({
59
+ public_key: pseudoKey,
60
+ provider_class: 'unknown',
61
+ environment: env,
62
+ }),
63
+ });
64
+ if (res.ok) {
65
+ const data = await res.json();
66
+ this._agentId = data.agent_id;
67
+ this._agentName = data.name;
68
+ return this._agentId;
69
+ }
70
+ // Registration failed — use pseudo ID locally
71
+ this._agentId = `pseudo_${randomBytes(6).toString('hex')}`;
72
+ return this._agentId;
73
+ }
74
+ finally {
75
+ this._registering = false;
76
+ }
77
+ }
78
+ }
79
+ /** Default singleton session for stdio mode. */
80
+ export const defaultSession = new SessionState('stdio');
81
+ //# sourceMappingURL=session-state.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session-state.js","sourceRoot":"","sources":["../../src/session-state.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAEpD,MAAM,OAAO,YAAY;IACf,QAAQ,GAAkB,IAAI,CAAC;IAC/B,UAAU,GAAkB,IAAI,CAAC;IACjC,YAAY,GAAG,KAAK,CAAC;IACrB,cAAc,CAA8B;IAC5C,WAAW,GAAkB,IAAI,CAAC;IAE1C,YAAY,gBAA6C,OAAO;QAC9D,IAAI,CAAC,cAAc,GAAG,aAAa,CAAC;IACtC,CAAC;IAED,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAED,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,UAAU,CAA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;oBACnB,UAAU,EAAE,SAAS;oBACrB,cAAc,EAAE,SAAS;oBACzB,WAAW,EAAE,GAAG;iBACjB,CAAC;aACH,CAAC,CAAC;YAEH,IAAI,GAAG,CAAC,EAAE,EAAE,CAAC;gBACX,MAAM,IAAI,GAAG,MAAM,GAAG,CAAC,IAAI,EAAwC,CAAC;gBACpE,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC;gBAC9B,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,IAAI,CAAC;gBAC5B,OAAO,IAAI,CAAC,QAAQ,CAAC;YACvB,CAAC;YAED,8CAA8C;YAC9C,IAAI,CAAC,QAAQ,GAAG,UAAU,WAAW,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;YAC3D,OAAO,IAAI,CAAC,QAAQ,CAAC;QACvB,CAAC;gBAAS,CAAC;YACT,IAAI,CAAC,YAAY,GAAG,KAAK,CAAC;QAC5B,CAAC;IACH,CAAC;CACF;AAED,gDAAgD;AAChD,MAAM,CAAC,MAAM,cAAc,GAAG,IAAI,YAAY,CAAC,OAAO,CAAC,CAAC"}
@@ -1,8 +1,6 @@
1
+ export declare function getApiUrl(): string;
1
2
  export declare function getAgentId(): string | null;
3
+ export declare function getAgentName(): string | null;
2
4
  export declare function setAgentId(id: string): void;
3
- /**
4
- * Ensure the agent is registered. Called before any tool that needs an agent_id.
5
- * If not registered, auto-registers with a pseudo_ ID.
6
- * Returns the agent_id.
7
- */
5
+ export declare function setAgentName(name: string): void;
8
6
  export declare function ensureRegistered(): Promise<string>;
package/dist/src/state.js CHANGED
@@ -1,53 +1,26 @@
1
1
  /**
2
- * MCP server session state.
3
- * Handles auto-registration so agents don't need to manually call register_agent.
2
+ * Backwards-compatible state module.
3
+ * Delegates to the defaultSession singleton from session-state.ts.
4
+ * Tools continue to import from './state.js' without changes.
4
5
  */
5
- import { randomBytes } from 'node:crypto';
6
- let agentId = null;
7
- let registering = false;
6
+ import { defaultSession } from './session-state.js';
8
7
  const ACR_API_URL = process.env.ACR_API_URL ?? 'https://acr.nfkey.ai';
8
+ export function getApiUrl() {
9
+ return ACR_API_URL;
10
+ }
9
11
  export function getAgentId() {
10
- return agentId;
12
+ return defaultSession.agentId;
13
+ }
14
+ export function getAgentName() {
15
+ return defaultSession.agentName;
11
16
  }
12
17
  export function setAgentId(id) {
13
- agentId = id;
18
+ defaultSession.setAgentId(id);
19
+ }
20
+ export function setAgentName(name) {
21
+ defaultSession.setAgentName(name);
14
22
  }
15
- /**
16
- * Ensure the agent is registered. Called before any tool that needs an agent_id.
17
- * If not registered, auto-registers with a pseudo_ ID.
18
- * Returns the agent_id.
19
- */
20
23
  export async function ensureRegistered() {
21
- if (agentId)
22
- return agentId;
23
- if (registering) {
24
- // Another call is already registering — wait briefly
25
- await new Promise((r) => setTimeout(r, 1000));
26
- if (agentId)
27
- return agentId;
28
- }
29
- registering = true;
30
- try {
31
- const pseudoKey = `pseudo_${randomBytes(16).toString('hex')}`;
32
- const res = await fetch(`${ACR_API_URL}/api/v1/register`, {
33
- method: 'POST',
34
- headers: { 'Content-Type': 'application/json' },
35
- body: JSON.stringify({
36
- public_key: pseudoKey,
37
- provider_class: 'unknown',
38
- }),
39
- });
40
- if (res.ok) {
41
- const data = await res.json();
42
- agentId = data.agent_id;
43
- return agentId;
44
- }
45
- // Registration failed — use pseudo ID locally
46
- agentId = `pseudo_${randomBytes(6).toString('hex')}`;
47
- return agentId;
48
- }
49
- finally {
50
- registering = false;
51
- }
24
+ return defaultSession.ensureRegistered(ACR_API_URL);
52
25
  }
53
26
  //# sourceMappingURL=state.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"state.js","sourceRoot":"","sources":["../../src/state.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C,IAAI,OAAO,GAAkB,IAAI,CAAC;AAClC,IAAI,WAAW,GAAG,KAAK,CAAC;AAExB,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