@agledger/mcp-server 2.0.1 → 2.1.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.
- package/README.md +22 -22
- package/dist/api-client.js +1 -1
- package/dist/index.js +1 -1
- package/dist/server.d.ts +3 -0
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +84 -16
- package/dist/server.js.map +1 -1
- package/package.json +1 -1
- package/src/api-client.ts +1 -1
- package/src/index.ts +1 -1
- package/src/server.ts +109 -17
- package/tests/server.test.ts +130 -8
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
The official [MCP](https://modelcontextprotocol.io) server for the [AGLedger](https://agledger.ai) API -- accountability and audit infrastructure for agentic systems.
|
|
4
4
|
|
|
5
|
-
Connects any MCP-compatible AI agent (Claude, Cursor, Windsurf, etc.) to the AGLedger API with
|
|
5
|
+
Connects any MCP-compatible AI agent (Claude, Cursor, Windsurf, etc.) to the AGLedger API with just 2 universal tools. No SDK code required -- just point your agent at this server.
|
|
6
6
|
|
|
7
7
|
## Install
|
|
8
8
|
|
|
@@ -19,10 +19,7 @@ Add to your MCP client configuration (e.g. `claude_desktop_config.json`):
|
|
|
19
19
|
"mcpServers": {
|
|
20
20
|
"agledger": {
|
|
21
21
|
"command": "agledger-mcp",
|
|
22
|
-
"args": ["--
|
|
23
|
-
"env": {
|
|
24
|
-
"AGLEDGER_API_KEY": "your-api-key"
|
|
25
|
-
}
|
|
22
|
+
"args": ["--api-key", "your-api-key"]
|
|
26
23
|
}
|
|
27
24
|
}
|
|
28
25
|
}
|
|
@@ -31,23 +28,26 @@ Add to your MCP client configuration (e.g. `claude_desktop_config.json`):
|
|
|
31
28
|
Or run directly:
|
|
32
29
|
|
|
33
30
|
```bash
|
|
34
|
-
agledger-mcp --api-key <key> --
|
|
31
|
+
agledger-mcp --api-key <key> [--api-url <url>]
|
|
35
32
|
```
|
|
36
33
|
|
|
37
|
-
##
|
|
34
|
+
## Tools
|
|
38
35
|
|
|
39
|
-
|
|
36
|
+
| Tool | Description |
|
|
37
|
+
|------|-------------|
|
|
38
|
+
| `agledger_discover` | Returns API health, your identity, available scopes, and a quickstart workflow. Call this first. |
|
|
39
|
+
| `agledger_api` | Make any AGLedger API call (method, path, params). The API returns `nextSteps` on every response for self-guided workflow discovery. |
|
|
40
40
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
41
|
+
### Agent workflow
|
|
42
|
+
|
|
43
|
+
The `agledger_discover` tool returns a quickstart workflow that guides agents through the accountability flow:
|
|
44
|
+
|
|
45
|
+
1. `GET /v1/schemas` -- list available contract types
|
|
46
|
+
2. `GET /v1/schemas/{type}` -- get required fields and examples
|
|
47
|
+
3. `POST /v1/mandates` -- create a mandate
|
|
48
|
+
4. `POST /v1/mandates/{id}/receipts` -- submit evidence when done
|
|
49
|
+
|
|
50
|
+
Every API error response includes a `suggestion` field with actionable recovery guidance -- agents can self-correct without human intervention.
|
|
51
51
|
|
|
52
52
|
## Configuration
|
|
53
53
|
|
|
@@ -55,8 +55,6 @@ The `--profile` flag is required. Each profile exposes a curated set of tools op
|
|
|
55
55
|
|------|---------|-------------|
|
|
56
56
|
| `--api-key` | `AGLEDGER_API_KEY` | AGLedger API key (required) |
|
|
57
57
|
| `--api-url` | `AGLEDGER_API_URL` | API base URL (default: `https://agledger.example.com`) |
|
|
58
|
-
| `--profile` | `AGLEDGER_PROFILE` | Tool profile (required) |
|
|
59
|
-
| `--enterprise-id` | `AGLEDGER_ENTERPRISE_ID` | Enterprise ID for admin/enterprise tools |
|
|
60
58
|
|
|
61
59
|
## What is AGLedger?
|
|
62
60
|
|
|
@@ -67,12 +65,14 @@ AGLedger is the accountability layer for automated operations. It records what w
|
|
|
67
65
|
- **Verdicts** -- principal acceptance decisions (PASS/FAIL) with settlement signals
|
|
68
66
|
- **Audit trail** -- hash-chained, Ed25519-signed, tamper-evident record
|
|
69
67
|
|
|
70
|
-
Learn more at [agledger.ai](https://agledger.ai) | [API docs](https://
|
|
68
|
+
Learn more at [agledger.ai](https://www.agledger.ai) | [API docs](https://www.agledger.ai/docs/)
|
|
69
|
+
|
|
70
|
+
Each self-hosted AGLedger instance also serves interactive Swagger UI at `{AGLEDGER_API_URL}/docs`.
|
|
71
71
|
|
|
72
72
|
## Requirements
|
|
73
73
|
|
|
74
74
|
- Node.js >= 22
|
|
75
|
-
-
|
|
75
|
+
- A running self-hosted AGLedger API instance and an API key (see the self-hosted install guide at [agledger.ai](https://www.agledger.ai))
|
|
76
76
|
|
|
77
77
|
## License
|
|
78
78
|
|
package/dist/api-client.js
CHANGED
|
@@ -19,7 +19,7 @@ export class ApiClient {
|
|
|
19
19
|
const headers = {
|
|
20
20
|
Authorization: `Bearer ${this.apiKey}`,
|
|
21
21
|
Accept: 'application/json',
|
|
22
|
-
'User-Agent': 'agledger-mcp-server/2.0
|
|
22
|
+
'User-Agent': 'agledger-mcp-server/2.1.0',
|
|
23
23
|
};
|
|
24
24
|
if (options?.body !== undefined) {
|
|
25
25
|
headers['Content-Type'] = 'application/json';
|
package/dist/index.js
CHANGED
package/dist/server.d.ts
CHANGED
|
@@ -3,11 +3,14 @@ import { ApiClient } from './api-client.js';
|
|
|
3
3
|
export interface AgledgerMcpServerOptions {
|
|
4
4
|
apiKey: string;
|
|
5
5
|
apiUrl?: string;
|
|
6
|
+
/** Request timeout in milliseconds. Default 30000. */
|
|
7
|
+
timeoutMs?: number;
|
|
6
8
|
}
|
|
7
9
|
export declare class AgledgerMcpServer {
|
|
8
10
|
readonly mcp: McpServer;
|
|
9
11
|
readonly client: ApiClient;
|
|
10
12
|
constructor(options: AgledgerMcpServerOptions);
|
|
11
13
|
private registerTools;
|
|
14
|
+
private registerResources;
|
|
12
15
|
}
|
|
13
16
|
//# sourceMappingURL=server.d.ts.map
|
package/dist/server.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AAEpE,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,MAAM,WAAW,wBAAwB;IACvC,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AAEpE,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,MAAM,WAAW,wBAAwB;IACvC,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,sDAAsD;IACtD,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAoCD,qBAAa,iBAAiB;IAC5B,QAAQ,CAAC,GAAG,EAAE,SAAS,CAAC;IACxB,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAC;gBAEf,OAAO,EAAE,wBAAwB;IAc7C,OAAO,CAAC,aAAa;IA0JrB,OAAO,CAAC,iBAAiB;CAkC1B"}
|
package/dist/server.js
CHANGED
|
@@ -1,25 +1,54 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
3
3
|
import { ApiClient } from './api-client.js';
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
const QUICKSTART = {
|
|
5
|
+
description: 'To track accountability for your work, follow these steps in order:',
|
|
6
|
+
steps: [
|
|
7
|
+
{ step: 1, action: 'List available contract types', method: 'GET', path: '/v1/schemas' },
|
|
8
|
+
{ step: 2, action: 'Get schema for your contract type', method: 'GET', path: '/v1/schemas/{contractType}' },
|
|
9
|
+
{ step: 3, action: 'Create a mandate', method: 'POST', path: '/v1/mandates' },
|
|
10
|
+
{ step: 4, action: 'Do your work, then submit a receipt', method: 'POST', path: '/v1/mandates/{id}/receipts' },
|
|
11
|
+
],
|
|
12
|
+
};
|
|
13
|
+
const DOCS = {
|
|
14
|
+
description: 'Every API response includes nextSteps for self-guided workflow discovery. ' +
|
|
15
|
+
'Call GET /openapi.json (also exposed as the agledger://openapi resource) for the full route catalog.',
|
|
16
|
+
openapi: '/openapi.json',
|
|
17
|
+
openapiResource: 'agledger://openapi',
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Structured error for CLI-origin failures (network, timeout, malformed input).
|
|
21
|
+
* API-origin errors are forwarded verbatim without enrichment — if the API
|
|
22
|
+
* returns a suggestion, agents see it; if not, that's an API concern to fix upstream.
|
|
23
|
+
*/
|
|
24
|
+
function errorResult(message, code, suggestion) {
|
|
25
|
+
const structuredContent = { error: true, message };
|
|
26
|
+
if (code)
|
|
27
|
+
structuredContent.code = code;
|
|
28
|
+
if (suggestion)
|
|
29
|
+
structuredContent.suggestion = suggestion;
|
|
30
|
+
return {
|
|
31
|
+
content: [{ type: 'text', text: `Error: ${message}` }],
|
|
32
|
+
structuredContent,
|
|
33
|
+
isError: true,
|
|
34
|
+
};
|
|
6
35
|
}
|
|
7
36
|
export class AgledgerMcpServer {
|
|
8
37
|
mcp;
|
|
9
38
|
client;
|
|
10
39
|
constructor(options) {
|
|
11
40
|
const apiUrl = options.apiUrl ?? 'https://agledger.example.com';
|
|
12
|
-
this.client = new ApiClient(apiUrl, options.apiKey);
|
|
13
|
-
this.mcp = new McpServer({ name: 'agledger-mcp-server', version: '2.0
|
|
41
|
+
this.client = new ApiClient(apiUrl, options.apiKey, options.timeoutMs);
|
|
42
|
+
this.mcp = new McpServer({ name: 'agledger-mcp-server', version: '2.1.0' }, { capabilities: { tools: {}, resources: {} } });
|
|
14
43
|
this.registerTools();
|
|
44
|
+
this.registerResources();
|
|
15
45
|
}
|
|
16
46
|
registerTools() {
|
|
17
47
|
const client = this.client;
|
|
18
48
|
this.mcp.registerTool('agledger_discover', {
|
|
19
49
|
title: 'Discover AGLedger API',
|
|
20
|
-
description: 'Returns API health, your identity, and
|
|
21
|
-
'Call this first
|
|
22
|
-
'The response includes nextSteps guiding you to available endpoints.',
|
|
50
|
+
description: 'Returns API health, your identity, available scopes, and a quickstart workflow. ' +
|
|
51
|
+
'Call this first. The response tells you who you are and what to do next.',
|
|
23
52
|
inputSchema: {},
|
|
24
53
|
annotations: {
|
|
25
54
|
readOnlyHint: true,
|
|
@@ -52,21 +81,30 @@ export class AgledgerMcpServer {
|
|
|
52
81
|
: String(identity.reason),
|
|
53
82
|
};
|
|
54
83
|
}
|
|
84
|
+
result.quickstart = QUICKSTART;
|
|
85
|
+
result.docs = DOCS;
|
|
55
86
|
return { content: [], structuredContent: result };
|
|
56
87
|
}
|
|
57
88
|
catch (err) {
|
|
58
|
-
return errorResult(err instanceof Error ? err.message : String(err));
|
|
89
|
+
return errorResult(err instanceof Error ? err.message : String(err), 'DISCOVER_FAILED');
|
|
59
90
|
}
|
|
60
91
|
});
|
|
61
92
|
this.mcp.registerTool('agledger_api', {
|
|
62
93
|
title: 'AGLedger API',
|
|
63
|
-
description: 'Make any AGLedger API call.
|
|
64
|
-
'
|
|
65
|
-
'
|
|
94
|
+
description: 'Make any AGLedger API call. All paths start with /v1/. ' +
|
|
95
|
+
'The API returns nextSteps on every response — follow them. Workflow: ' +
|
|
96
|
+
'1. GET /v1/schemas — list contract types. ' +
|
|
97
|
+
'2. GET /v1/schemas/{type} — get required fields and examples. ' +
|
|
98
|
+
'3. POST /v1/mandates — create a mandate. ' +
|
|
99
|
+
'4. POST /v1/mandates/{id}/receipts — submit evidence when done. ' +
|
|
100
|
+
'If a call fails, read the suggestion field in the error response. ' +
|
|
101
|
+
'For the full API catalog, GET /openapi.json (or read the agledger://openapi resource). ' +
|
|
66
102
|
'For GET/DELETE, params become query parameters. For POST/PUT/PATCH, params become the JSON body.',
|
|
67
103
|
inputSchema: {
|
|
68
104
|
method: z.enum(['GET', 'POST', 'PUT', 'PATCH', 'DELETE']).describe('HTTP method'),
|
|
69
|
-
path: z
|
|
105
|
+
path: z
|
|
106
|
+
.string()
|
|
107
|
+
.describe('API path starting with / (e.g. /v1/mandates, /v1/schemas, /v1/mandates/{id}/receipts)'),
|
|
70
108
|
params: z
|
|
71
109
|
.record(z.string(), z.unknown())
|
|
72
110
|
.optional()
|
|
@@ -82,7 +120,8 @@ export class AgledgerMcpServer {
|
|
|
82
120
|
try {
|
|
83
121
|
const { method, path, params } = args;
|
|
84
122
|
if (!path.startsWith('/')) {
|
|
85
|
-
return errorResult('Path must start with /'
|
|
123
|
+
return errorResult('Path must start with /. All API routes use /v1/ prefix. ' +
|
|
124
|
+
'Example: /v1/mandates, /v1/schemas. Call agledger_discover for the full workflow.', 'PATH_INVALID', 'Prefix the path with /v1/ (e.g. /v1/mandates). Call agledger_discover if unsure which path to use.');
|
|
86
125
|
}
|
|
87
126
|
const options = {};
|
|
88
127
|
if (params) {
|
|
@@ -100,6 +139,8 @@ export class AgledgerMcpServer {
|
|
|
100
139
|
structuredContent: response.body,
|
|
101
140
|
};
|
|
102
141
|
}
|
|
142
|
+
// Forward the API error body verbatim. The API owns error guidance;
|
|
143
|
+
// the MCP does not enrich, translate, or inject suggestions.
|
|
103
144
|
const errorBody = (response.body ?? {});
|
|
104
145
|
const message = (errorBody.message ??
|
|
105
146
|
errorBody.error ??
|
|
@@ -112,14 +153,41 @@ export class AgledgerMcpServer {
|
|
|
112
153
|
}
|
|
113
154
|
catch (err) {
|
|
114
155
|
if (err instanceof DOMException && err.name === 'AbortError') {
|
|
115
|
-
return errorResult('
|
|
156
|
+
return errorResult('The API did not respond in time. Retry the same request.', 'TIMEOUT', 'Retry the same request. Increase --timeout if your instance is slow to respond.');
|
|
116
157
|
}
|
|
117
158
|
if (err instanceof TypeError && err.message.includes('fetch')) {
|
|
118
|
-
return errorResult(
|
|
159
|
+
return errorResult(err.message, 'NETWORK_ERROR', 'Check that the API URL is correct. Try GET /health to verify connectivity.');
|
|
119
160
|
}
|
|
120
|
-
return errorResult(err instanceof Error ? err.message : String(err));
|
|
161
|
+
return errorResult(err instanceof Error ? err.message : String(err), 'UNKNOWN_ERROR');
|
|
121
162
|
}
|
|
122
163
|
});
|
|
123
164
|
}
|
|
165
|
+
registerResources() {
|
|
166
|
+
const client = this.client;
|
|
167
|
+
/**
|
|
168
|
+
* Proxies GET /openapi.json at read time. Keeps the spec fresh — no local
|
|
169
|
+
* cache that could go stale. Clients (Claude Desktop, etc.) surface this
|
|
170
|
+
* in their resource picker, so users/agents can pull the spec into context
|
|
171
|
+
* without constructing an `agledger_api` call.
|
|
172
|
+
*/
|
|
173
|
+
this.mcp.registerResource('agledger-openapi', 'agledger://openapi', {
|
|
174
|
+
description: 'The AGLedger API OpenAPI 3.0 specification. Fetches live from GET /openapi.json — always current with the running API instance.',
|
|
175
|
+
mimeType: 'application/json',
|
|
176
|
+
}, async () => {
|
|
177
|
+
const response = await client.request('GET', '/openapi.json');
|
|
178
|
+
if (!response.ok) {
|
|
179
|
+
throw new Error(`Failed to fetch /openapi.json: HTTP ${response.status}`);
|
|
180
|
+
}
|
|
181
|
+
return {
|
|
182
|
+
contents: [
|
|
183
|
+
{
|
|
184
|
+
uri: 'agledger://openapi',
|
|
185
|
+
mimeType: 'application/json',
|
|
186
|
+
text: typeof response.body === 'string' ? response.body : JSON.stringify(response.body),
|
|
187
|
+
},
|
|
188
|
+
],
|
|
189
|
+
};
|
|
190
|
+
});
|
|
191
|
+
}
|
|
124
192
|
}
|
|
125
193
|
//# sourceMappingURL=server.js.map
|
package/dist/server.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AAEp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GAAG,CAAC,EAAE,eAAe,CAAC,CAAC;YACxF,CAAC;QACH,CAAC,CACF,CAAC;IACJ,CAAC;IAEO,iBAAiB;QACvB,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;QAE3B;;;;;WAKG;QACH,IAAI,CAAC,GAAG,CAAC,gBAAgB,CACvB,kBAAkB,EAClB,oBAAoB,EACpB;YACE,WAAW,EACT,iIAAiI;YACnI,QAAQ,EAAE,kBAAkB;SAC7B,EACD,KAAK,IAAI,EAAE;YACT,MAAM,QAAQ,GAAG,MAAM,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,eAAe,CAAC,CAAC;YAC9D,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;gBACjB,MAAM,IAAI,KAAK,CAAC,uCAAuC,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC;YAC5E,CAAC;YACD,OAAO;gBACL,QAAQ,EAAE;oBACR;wBACE,GAAG,EAAE,oBAAoB;wBACzB,QAAQ,EAAE,kBAAkB;wBAC5B,IAAI,EAAE,OAAO,QAAQ,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC;qBACxF;iBACF;aACF,CAAC;QACJ,CAAC,CACF,CAAC;IACJ,CAAC;CACF"}
|
package/package.json
CHANGED
package/src/api-client.ts
CHANGED
|
@@ -36,7 +36,7 @@ export class ApiClient {
|
|
|
36
36
|
const headers: Record<string, string> = {
|
|
37
37
|
Authorization: `Bearer ${this.apiKey}`,
|
|
38
38
|
Accept: 'application/json',
|
|
39
|
-
'User-Agent': 'agledger-mcp-server/2.0
|
|
39
|
+
'User-Agent': 'agledger-mcp-server/2.1.0',
|
|
40
40
|
};
|
|
41
41
|
|
|
42
42
|
if (options?.body !== undefined) {
|
package/src/index.ts
CHANGED
package/src/server.ts
CHANGED
|
@@ -6,10 +6,42 @@ import { ApiClient } from './api-client.js';
|
|
|
6
6
|
export interface AgledgerMcpServerOptions {
|
|
7
7
|
apiKey: string;
|
|
8
8
|
apiUrl?: string;
|
|
9
|
+
/** Request timeout in milliseconds. Default 30000. */
|
|
10
|
+
timeoutMs?: number;
|
|
9
11
|
}
|
|
10
12
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
+
const QUICKSTART = {
|
|
14
|
+
description: 'To track accountability for your work, follow these steps in order:',
|
|
15
|
+
steps: [
|
|
16
|
+
{ step: 1, action: 'List available contract types', method: 'GET', path: '/v1/schemas' },
|
|
17
|
+
{ step: 2, action: 'Get schema for your contract type', method: 'GET', path: '/v1/schemas/{contractType}' },
|
|
18
|
+
{ step: 3, action: 'Create a mandate', method: 'POST', path: '/v1/mandates' },
|
|
19
|
+
{ step: 4, action: 'Do your work, then submit a receipt', method: 'POST', path: '/v1/mandates/{id}/receipts' },
|
|
20
|
+
],
|
|
21
|
+
} as const;
|
|
22
|
+
|
|
23
|
+
const DOCS = {
|
|
24
|
+
description:
|
|
25
|
+
'Every API response includes nextSteps for self-guided workflow discovery. ' +
|
|
26
|
+
'Call GET /openapi.json (also exposed as the agledger://openapi resource) for the full route catalog.',
|
|
27
|
+
openapi: '/openapi.json',
|
|
28
|
+
openapiResource: 'agledger://openapi',
|
|
29
|
+
} as const;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Structured error for CLI-origin failures (network, timeout, malformed input).
|
|
33
|
+
* API-origin errors are forwarded verbatim without enrichment — if the API
|
|
34
|
+
* returns a suggestion, agents see it; if not, that's an API concern to fix upstream.
|
|
35
|
+
*/
|
|
36
|
+
function errorResult(message: string, code?: string, suggestion?: string): CallToolResult {
|
|
37
|
+
const structuredContent: Record<string, unknown> = { error: true, message };
|
|
38
|
+
if (code) structuredContent.code = code;
|
|
39
|
+
if (suggestion) structuredContent.suggestion = suggestion;
|
|
40
|
+
return {
|
|
41
|
+
content: [{ type: 'text', text: `Error: ${message}` }],
|
|
42
|
+
structuredContent,
|
|
43
|
+
isError: true,
|
|
44
|
+
};
|
|
13
45
|
}
|
|
14
46
|
|
|
15
47
|
export class AgledgerMcpServer {
|
|
@@ -19,14 +51,15 @@ export class AgledgerMcpServer {
|
|
|
19
51
|
constructor(options: AgledgerMcpServerOptions) {
|
|
20
52
|
const apiUrl = options.apiUrl ?? 'https://agledger.example.com';
|
|
21
53
|
|
|
22
|
-
this.client = new ApiClient(apiUrl, options.apiKey);
|
|
54
|
+
this.client = new ApiClient(apiUrl, options.apiKey, options.timeoutMs);
|
|
23
55
|
|
|
24
56
|
this.mcp = new McpServer(
|
|
25
|
-
{ name: 'agledger-mcp-server', version: '2.0
|
|
26
|
-
{ capabilities: { tools: {} } },
|
|
57
|
+
{ name: 'agledger-mcp-server', version: '2.1.0' },
|
|
58
|
+
{ capabilities: { tools: {}, resources: {} } },
|
|
27
59
|
);
|
|
28
60
|
|
|
29
61
|
this.registerTools();
|
|
62
|
+
this.registerResources();
|
|
30
63
|
}
|
|
31
64
|
|
|
32
65
|
private registerTools(): void {
|
|
@@ -37,9 +70,8 @@ export class AgledgerMcpServer {
|
|
|
37
70
|
{
|
|
38
71
|
title: 'Discover AGLedger API',
|
|
39
72
|
description:
|
|
40
|
-
'Returns API health, your identity, and
|
|
41
|
-
'Call this first
|
|
42
|
-
'The response includes nextSteps guiding you to available endpoints.',
|
|
73
|
+
'Returns API health, your identity, available scopes, and a quickstart workflow. ' +
|
|
74
|
+
'Call this first. The response tells you who you are and what to do next.',
|
|
43
75
|
inputSchema: {},
|
|
44
76
|
annotations: {
|
|
45
77
|
readOnlyHint: true,
|
|
@@ -76,9 +108,12 @@ export class AgledgerMcpServer {
|
|
|
76
108
|
};
|
|
77
109
|
}
|
|
78
110
|
|
|
111
|
+
result.quickstart = QUICKSTART;
|
|
112
|
+
result.docs = DOCS;
|
|
113
|
+
|
|
79
114
|
return { content: [], structuredContent: result };
|
|
80
115
|
} catch (err) {
|
|
81
|
-
return errorResult(err instanceof Error ? err.message : String(err));
|
|
116
|
+
return errorResult(err instanceof Error ? err.message : String(err), 'DISCOVER_FAILED');
|
|
82
117
|
}
|
|
83
118
|
},
|
|
84
119
|
);
|
|
@@ -88,13 +123,20 @@ export class AgledgerMcpServer {
|
|
|
88
123
|
{
|
|
89
124
|
title: 'AGLedger API',
|
|
90
125
|
description:
|
|
91
|
-
'Make any AGLedger API call.
|
|
92
|
-
'
|
|
93
|
-
'
|
|
126
|
+
'Make any AGLedger API call. All paths start with /v1/. ' +
|
|
127
|
+
'The API returns nextSteps on every response — follow them. Workflow: ' +
|
|
128
|
+
'1. GET /v1/schemas — list contract types. ' +
|
|
129
|
+
'2. GET /v1/schemas/{type} — get required fields and examples. ' +
|
|
130
|
+
'3. POST /v1/mandates — create a mandate. ' +
|
|
131
|
+
'4. POST /v1/mandates/{id}/receipts — submit evidence when done. ' +
|
|
132
|
+
'If a call fails, read the suggestion field in the error response. ' +
|
|
133
|
+
'For the full API catalog, GET /openapi.json (or read the agledger://openapi resource). ' +
|
|
94
134
|
'For GET/DELETE, params become query parameters. For POST/PUT/PATCH, params become the JSON body.',
|
|
95
135
|
inputSchema: {
|
|
96
136
|
method: z.enum(['GET', 'POST', 'PUT', 'PATCH', 'DELETE']).describe('HTTP method'),
|
|
97
|
-
path: z
|
|
137
|
+
path: z
|
|
138
|
+
.string()
|
|
139
|
+
.describe('API path starting with / (e.g. /v1/mandates, /v1/schemas, /v1/mandates/{id}/receipts)'),
|
|
98
140
|
params: z
|
|
99
141
|
.record(z.string(), z.unknown())
|
|
100
142
|
.optional()
|
|
@@ -114,7 +156,12 @@ export class AgledgerMcpServer {
|
|
|
114
156
|
const { method, path, params } = args;
|
|
115
157
|
|
|
116
158
|
if (!path.startsWith('/')) {
|
|
117
|
-
return errorResult(
|
|
159
|
+
return errorResult(
|
|
160
|
+
'Path must start with /. All API routes use /v1/ prefix. ' +
|
|
161
|
+
'Example: /v1/mandates, /v1/schemas. Call agledger_discover for the full workflow.',
|
|
162
|
+
'PATH_INVALID',
|
|
163
|
+
'Prefix the path with /v1/ (e.g. /v1/mandates). Call agledger_discover if unsure which path to use.',
|
|
164
|
+
);
|
|
118
165
|
}
|
|
119
166
|
|
|
120
167
|
const options: { query?: Record<string, unknown>; body?: unknown } = {};
|
|
@@ -136,6 +183,8 @@ export class AgledgerMcpServer {
|
|
|
136
183
|
};
|
|
137
184
|
}
|
|
138
185
|
|
|
186
|
+
// Forward the API error body verbatim. The API owns error guidance;
|
|
187
|
+
// the MCP does not enrich, translate, or inject suggestions.
|
|
139
188
|
const errorBody = (response.body ?? {}) as Record<string, unknown>;
|
|
140
189
|
const message = (errorBody.message ??
|
|
141
190
|
errorBody.error ??
|
|
@@ -148,13 +197,56 @@ export class AgledgerMcpServer {
|
|
|
148
197
|
};
|
|
149
198
|
} catch (err) {
|
|
150
199
|
if (err instanceof DOMException && err.name === 'AbortError') {
|
|
151
|
-
return errorResult(
|
|
200
|
+
return errorResult(
|
|
201
|
+
'The API did not respond in time. Retry the same request.',
|
|
202
|
+
'TIMEOUT',
|
|
203
|
+
'Retry the same request. Increase --timeout if your instance is slow to respond.',
|
|
204
|
+
);
|
|
152
205
|
}
|
|
153
206
|
if (err instanceof TypeError && err.message.includes('fetch')) {
|
|
154
|
-
return errorResult(
|
|
207
|
+
return errorResult(
|
|
208
|
+
err.message,
|
|
209
|
+
'NETWORK_ERROR',
|
|
210
|
+
'Check that the API URL is correct. Try GET /health to verify connectivity.',
|
|
211
|
+
);
|
|
155
212
|
}
|
|
156
|
-
return errorResult(err instanceof Error ? err.message : String(err));
|
|
213
|
+
return errorResult(err instanceof Error ? err.message : String(err), 'UNKNOWN_ERROR');
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
private registerResources(): void {
|
|
220
|
+
const client = this.client;
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Proxies GET /openapi.json at read time. Keeps the spec fresh — no local
|
|
224
|
+
* cache that could go stale. Clients (Claude Desktop, etc.) surface this
|
|
225
|
+
* in their resource picker, so users/agents can pull the spec into context
|
|
226
|
+
* without constructing an `agledger_api` call.
|
|
227
|
+
*/
|
|
228
|
+
this.mcp.registerResource(
|
|
229
|
+
'agledger-openapi',
|
|
230
|
+
'agledger://openapi',
|
|
231
|
+
{
|
|
232
|
+
description:
|
|
233
|
+
'The AGLedger API OpenAPI 3.0 specification. Fetches live from GET /openapi.json — always current with the running API instance.',
|
|
234
|
+
mimeType: 'application/json',
|
|
235
|
+
},
|
|
236
|
+
async () => {
|
|
237
|
+
const response = await client.request('GET', '/openapi.json');
|
|
238
|
+
if (!response.ok) {
|
|
239
|
+
throw new Error(`Failed to fetch /openapi.json: HTTP ${response.status}`);
|
|
157
240
|
}
|
|
241
|
+
return {
|
|
242
|
+
contents: [
|
|
243
|
+
{
|
|
244
|
+
uri: 'agledger://openapi',
|
|
245
|
+
mimeType: 'application/json',
|
|
246
|
+
text: typeof response.body === 'string' ? response.body : JSON.stringify(response.body),
|
|
247
|
+
},
|
|
248
|
+
],
|
|
249
|
+
};
|
|
158
250
|
},
|
|
159
251
|
);
|
|
160
252
|
}
|
package/tests/server.test.ts
CHANGED
|
@@ -28,12 +28,62 @@ describe('tool registration', () => {
|
|
|
28
28
|
expect(tool).toBeDefined();
|
|
29
29
|
expect(tool!.description).toContain('health');
|
|
30
30
|
expect(tool!.description).toContain('identity');
|
|
31
|
+
expect(tool!.description).toContain('quickstart');
|
|
31
32
|
});
|
|
32
33
|
|
|
33
34
|
it('registers agledger_api', () => {
|
|
34
35
|
const tool = tools.find((t) => t.name === 'agledger_api');
|
|
35
36
|
expect(tool).toBeDefined();
|
|
36
37
|
expect(tool!.description).toContain('nextSteps');
|
|
38
|
+
expect(tool!.description).toContain('/v1/');
|
|
39
|
+
expect(tool!.description).toContain('/v1/schemas');
|
|
40
|
+
expect(tool!.description).toContain('/v1/mandates');
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
describe('resource registration', () => {
|
|
45
|
+
it('registers the agledger://openapi resource', async () => {
|
|
46
|
+
const { resources } = await harness.client.listResources();
|
|
47
|
+
const openapi = resources.find((r) => r.uri === 'agledger://openapi');
|
|
48
|
+
expect(openapi).toBeDefined();
|
|
49
|
+
expect(openapi!.mimeType).toBe('application/json');
|
|
50
|
+
expect(openapi!.description).toContain('OpenAPI');
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('reads the openapi resource by proxying to GET /openapi.json', async () => {
|
|
54
|
+
const spec = { openapi: '3.0.3', info: { title: 'AGLedger API' }, paths: {} };
|
|
55
|
+
const mockFetch = vi.fn().mockResolvedValueOnce(
|
|
56
|
+
new Response(JSON.stringify(spec), {
|
|
57
|
+
status: 200,
|
|
58
|
+
headers: { 'content-type': 'application/json' },
|
|
59
|
+
}),
|
|
60
|
+
);
|
|
61
|
+
vi.stubGlobal('fetch', mockFetch);
|
|
62
|
+
|
|
63
|
+
const result = await harness.client.readResource({ uri: 'agledger://openapi' });
|
|
64
|
+
expect(result.contents).toHaveLength(1);
|
|
65
|
+
const content = result.contents[0];
|
|
66
|
+
expect(content.uri).toBe('agledger://openapi');
|
|
67
|
+
expect(content.mimeType).toBe('application/json');
|
|
68
|
+
expect(JSON.parse(content.text as string)).toEqual(spec);
|
|
69
|
+
|
|
70
|
+
// Confirm the proxy hit the right endpoint
|
|
71
|
+
const url = mockFetch.mock.calls[0][0] as string;
|
|
72
|
+
expect(url).toContain('/openapi.json');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('surfaces upstream failure when /openapi.json is not reachable', async () => {
|
|
76
|
+
vi.stubGlobal(
|
|
77
|
+
'fetch',
|
|
78
|
+
vi.fn().mockResolvedValueOnce(
|
|
79
|
+
new Response('not found', {
|
|
80
|
+
status: 404,
|
|
81
|
+
headers: { 'content-type': 'text/plain' },
|
|
82
|
+
}),
|
|
83
|
+
),
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
await expect(harness.client.readResource({ uri: 'agledger://openapi' })).rejects.toThrow();
|
|
37
87
|
});
|
|
38
88
|
});
|
|
39
89
|
|
|
@@ -61,6 +111,18 @@ describe('agledger_discover', () => {
|
|
|
61
111
|
const content = result.structuredContent as Record<string, unknown>;
|
|
62
112
|
expect(content.health).toEqual({ status: 'ok', version: '1.0.0' });
|
|
63
113
|
expect(content.identity).toEqual({ agentId: 'agent-1', scopes: ['mandates:read'] });
|
|
114
|
+
|
|
115
|
+
// Quickstart workflow is always present
|
|
116
|
+
const quickstart = content.quickstart as { steps: Array<{ step: number; path: string }> };
|
|
117
|
+
expect(quickstart.steps).toHaveLength(4);
|
|
118
|
+
expect(quickstart.steps[0].path).toBe('/v1/schemas');
|
|
119
|
+
expect(quickstart.steps[2].path).toBe('/v1/mandates');
|
|
120
|
+
|
|
121
|
+
// Docs hints always present — point at live API discovery and the openapi resource.
|
|
122
|
+
const docs = content.docs as { openapi: string; openapiResource: string; description: string };
|
|
123
|
+
expect(docs.openapi).toBe('/openapi.json');
|
|
124
|
+
expect(docs.openapiResource).toBe('agledger://openapi');
|
|
125
|
+
expect(docs.description).toContain('nextSteps');
|
|
64
126
|
});
|
|
65
127
|
|
|
66
128
|
it('returns partial results when one call fails', async () => {
|
|
@@ -192,18 +254,76 @@ describe('agledger_api', () => {
|
|
|
192
254
|
expect(content.missingScopes).toEqual(['mandates:write']);
|
|
193
255
|
});
|
|
194
256
|
|
|
195
|
-
it('rejects path not starting with /', async () => {
|
|
257
|
+
it('rejects path not starting with / with recovery directive', async () => {
|
|
196
258
|
const result = await harness.client.callTool({
|
|
197
259
|
name: 'agledger_api',
|
|
198
260
|
arguments: { method: 'GET', path: 'mandates' },
|
|
199
261
|
});
|
|
200
262
|
|
|
201
263
|
expect(result.isError).toBe(true);
|
|
264
|
+
const content = result.structuredContent as Record<string, unknown>;
|
|
265
|
+
expect(content.code).toBe('PATH_INVALID');
|
|
202
266
|
const text = (result.content as Array<{ type: string; text: string }>)[0].text;
|
|
203
|
-
expect(text).toContain('
|
|
267
|
+
expect(text).toContain('/v1/');
|
|
268
|
+
expect(text).toContain('agledger_discover');
|
|
269
|
+
expect(content.suggestion).toBeDefined();
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it('forwards API error body verbatim when suggestion is missing (no MCP-side enrichment)', async () => {
|
|
273
|
+
// Thin-passthrough contract: the API owns error guidance. The MCP must not
|
|
274
|
+
// inject a suggestion or any other field the API didn't return.
|
|
275
|
+
const errorBody = {
|
|
276
|
+
message: 'Invalid contract type',
|
|
277
|
+
code: 'BAD_REQUEST',
|
|
278
|
+
};
|
|
279
|
+
vi.stubGlobal(
|
|
280
|
+
'fetch',
|
|
281
|
+
vi.fn().mockResolvedValueOnce(
|
|
282
|
+
new Response(JSON.stringify(errorBody), {
|
|
283
|
+
status: 400,
|
|
284
|
+
headers: { 'content-type': 'application/json' },
|
|
285
|
+
}),
|
|
286
|
+
),
|
|
287
|
+
);
|
|
288
|
+
|
|
289
|
+
const result = await harness.client.callTool({
|
|
290
|
+
name: 'agledger_api',
|
|
291
|
+
arguments: { method: 'POST', path: '/v1/mandates', params: { contractType: 'INVALID' } },
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
expect(result.isError).toBe(true);
|
|
295
|
+
const content = result.structuredContent as Record<string, unknown>;
|
|
296
|
+
expect(content).toEqual(errorBody); // exact match — no added fields
|
|
297
|
+
expect(content.suggestion).toBeUndefined();
|
|
204
298
|
});
|
|
205
299
|
|
|
206
|
-
it('
|
|
300
|
+
it('preserves existing API suggestion without overwriting', async () => {
|
|
301
|
+
const errorBody = {
|
|
302
|
+
message: 'Mandate not found',
|
|
303
|
+
code: 'NOT_FOUND',
|
|
304
|
+
suggestion: 'The mandate may have been deleted.',
|
|
305
|
+
};
|
|
306
|
+
vi.stubGlobal(
|
|
307
|
+
'fetch',
|
|
308
|
+
vi.fn().mockResolvedValueOnce(
|
|
309
|
+
new Response(JSON.stringify(errorBody), {
|
|
310
|
+
status: 404,
|
|
311
|
+
headers: { 'content-type': 'application/json' },
|
|
312
|
+
}),
|
|
313
|
+
),
|
|
314
|
+
);
|
|
315
|
+
|
|
316
|
+
const result = await harness.client.callTool({
|
|
317
|
+
name: 'agledger_api',
|
|
318
|
+
arguments: { method: 'GET', path: '/v1/mandates/bad-id' },
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
expect(result.isError).toBe(true);
|
|
322
|
+
const content = result.structuredContent as Record<string, unknown>;
|
|
323
|
+
expect(content.suggestion).toBe('The mandate may have been deleted.');
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it('handles network errors with recovery directive', async () => {
|
|
207
327
|
vi.stubGlobal(
|
|
208
328
|
'fetch',
|
|
209
329
|
vi.fn().mockRejectedValueOnce(new TypeError('fetch failed: DNS resolution failed')),
|
|
@@ -215,11 +335,12 @@ describe('agledger_api', () => {
|
|
|
215
335
|
});
|
|
216
336
|
|
|
217
337
|
expect(result.isError).toBe(true);
|
|
218
|
-
const
|
|
219
|
-
expect(
|
|
338
|
+
const content = result.structuredContent as Record<string, unknown>;
|
|
339
|
+
expect(content.code).toBe('NETWORK_ERROR');
|
|
340
|
+
expect(content.suggestion).toContain('/health');
|
|
220
341
|
});
|
|
221
342
|
|
|
222
|
-
it('handles timeout
|
|
343
|
+
it('handles timeout with recovery directive', async () => {
|
|
223
344
|
vi.stubGlobal(
|
|
224
345
|
'fetch',
|
|
225
346
|
vi
|
|
@@ -235,8 +356,9 @@ describe('agledger_api', () => {
|
|
|
235
356
|
});
|
|
236
357
|
|
|
237
358
|
expect(result.isError).toBe(true);
|
|
238
|
-
const
|
|
239
|
-
expect(
|
|
359
|
+
const content = result.structuredContent as Record<string, unknown>;
|
|
360
|
+
expect(content.code).toBe('TIMEOUT');
|
|
361
|
+
expect(content.suggestion).toContain('Retry');
|
|
240
362
|
});
|
|
241
363
|
|
|
242
364
|
it('handles non-JSON responses', async () => {
|