@agledger/mcp-server 3.1.0 → 3.2.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/dist/index.js CHANGED
@@ -1,11 +1,13 @@
1
1
  #!/usr/bin/env node
2
2
  import { accessSync, constants as fsConstants } from 'node:fs';
3
+ import { resolve as resolvePath } from 'node:path';
3
4
  import { parseArgs } from 'node:util';
4
- import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
5
+ import { StdioServerTransport } from '@modelcontextprotocol/server/stdio';
5
6
  import { AgledgerMcpServer, SERVER_VERSION } from './server.js';
6
7
  import { resolveZodCopies, zodSplitWarning } from './zod-integrity.js';
7
- import { OIDC_ENV, oidcTokenFromCommand, oidcTokenFromFile } from './credentials.js';
8
+ import { OIDC_ENV, killRunningTokenCommands, oidcTokenFromCommand, oidcTokenFromFile } from './credentials.js';
8
9
  import { ON_BEHALF_OF_ENV } from './delegation.js';
10
+ import { errorMessage } from './errors.js';
9
11
  /**
10
12
  * Exit codes. A launcher that supervises this process, or a shell wrapping it,
11
13
  * has only the code to go on: stderr is where the MCP client's own diagnostics
@@ -14,17 +16,37 @@ import { ON_BEHALF_OF_ENV } from './delegation.js';
14
16
  */
15
17
  const EXIT_RUNTIME_FAILURE = 1;
16
18
  const EXIT_USAGE_ERROR = 2;
17
- // These handlers used to print and return, which SUPPRESSES Node's default
18
- // non-zero exit: an unstartable server reported its own fatal error and then
19
- // exited 0, so anything reading the exit code saw a clean shutdown.
19
+ // These handlers print the error and then exit non-zero explicitly: returning
20
+ // from an uncaughtException handler suppresses Node's default non-zero exit,
21
+ // so an unstartable server would otherwise report its fatal error and exit 0.
20
22
  process.on('uncaughtException', (err) => {
21
- process.stderr.write(`Fatal: ${err.message}\n`);
23
+ process.stderr.write(`Fatal: ${fatalText(err)}\n`);
22
24
  process.exit(EXIT_RUNTIME_FAILURE);
23
25
  });
24
26
  process.on('unhandledRejection', (reason) => {
25
- process.stderr.write(`Fatal: unhandled rejection: ${reason instanceof Error ? reason.message : String(reason)}\n`);
27
+ process.stderr.write(`Fatal: unhandled rejection: ${fatalText(reason)}\n`);
26
28
  process.exit(EXIT_RUNTIME_FAILURE);
27
29
  });
30
+ // A token command runs in its own process group, so it would outlive a server
31
+ // that is told to stop. End it first, then exit with the conventional code.
32
+ for (const [signal, code] of [['SIGINT', 130], ['SIGTERM', 143]]) {
33
+ process.on(signal, () => {
34
+ killRunningTokenCommands();
35
+ process.exit(code);
36
+ });
37
+ }
38
+ /**
39
+ * A fatal error as text: the message and its cause, or with DEBUG set the
40
+ * whole stack (which carries the message and, for a cause, the cause's).
41
+ */
42
+ function fatalText(err) {
43
+ if (!(err instanceof Error))
44
+ return String(err);
45
+ if (process.env.DEBUG)
46
+ return err.stack ?? err.message;
47
+ const cause = err.cause instanceof Error ? err.cause.message : typeof err.cause === 'string' ? err.cause : undefined;
48
+ return cause && !err.message.includes(cause) ? `${err.message} (${cause})` : err.message;
49
+ }
28
50
  /** Why an API URL cannot be used, or undefined when it can. */
29
51
  function apiUrlProblem(url) {
30
52
  let parsed;
@@ -46,6 +68,9 @@ function timeoutProblem(raw) {
46
68
  const seconds = raw.trim() === '' ? NaN : Number(raw);
47
69
  if (!Number.isFinite(seconds) || seconds <= 0)
48
70
  return 'is not a positive number of seconds';
71
+ // The wait is whole milliseconds; anything that rounds to 0 would time every call out at once.
72
+ if (Math.round(seconds * 1000) < 1)
73
+ return 'is less than 1 millisecond';
49
74
  if (seconds > MAX_TIMEOUT_SECONDS)
50
75
  return `is more than ${MAX_TIMEOUT_SECONDS} seconds`;
51
76
  return undefined;
@@ -59,6 +84,7 @@ function main() {
59
84
  'api-url': { type: 'string', short: 'u' },
60
85
  timeout: { type: 'string' },
61
86
  help: { type: 'boolean', short: 'h' },
87
+ version: { type: 'boolean', short: 'V' },
62
88
  },
63
89
  strict: true,
64
90
  }));
@@ -67,11 +93,18 @@ function main() {
67
93
  // strict parseArgs throws on an unknown flag. That is a usage error the
68
94
  // caller can fix, not a crash, so say so and point at --help rather than
69
95
  // letting it surface as an uncaught exception.
70
- process.stderr.write(`Error: ${err instanceof Error ? err.message : String(err)}\nRun \`agledger-mcp --help\` for usage.\n`);
96
+ process.stderr.write(`Error: ${errorMessage(err)}\nRun \`agledger-mcp --help\` for usage.\n`);
71
97
  process.exit(EXIT_USAGE_ERROR);
72
98
  }
99
+ if (values.version) {
100
+ // Nothing has been connected yet, so stdout is still free for a caller to
101
+ // capture; once the server starts it belongs to the MCP transport.
102
+ process.stdout.write(`agledger-mcp ${SERVER_VERSION}\n`);
103
+ process.exit(0);
104
+ }
73
105
  if (values.help) {
74
- process.stderr.write(`AGLedger MCP Server v${SERVER_VERSION}
106
+ // stdout, like --version: nothing has been connected yet, so it is still free.
107
+ process.stdout.write(`AGLedger MCP Server v${SERVER_VERSION}
75
108
 
76
109
  Usage: agledger-mcp --api-key <key> --api-url <url>
77
110
  AGLEDGER_OIDC_TOKEN_FILE=<path> agledger-mcp --api-url <url>
@@ -86,6 +119,7 @@ Options:
86
119
  out may still have been applied: retry a POST with the
87
120
  idempotencyKey the timeout error names, never a new one.
88
121
  --help, -h Show this help message
122
+ --version, -V Print the version and exit
89
123
 
90
124
  Credentials: one is required. An API key wins when set; otherwise an OIDC
91
125
  token source is exchanged for a short-lived cert (POST /v1/auth/oidc/cert),
@@ -104,6 +138,12 @@ request bodies. Nothing is written to disk.
104
138
  Bind an agent with PATCH /v1/agents/{id}
105
139
  (oidcIss, oidcSub) or claimMapping.agent_id
106
140
  on the trusted issuer.
141
+ AGLEDGER_AGENT_KEYS_FILE Optional. A JWK Set file the server's cert public
142
+ key is added to on each exchange, before the cert
143
+ is used, so its agent signatures can be
144
+ re-verified offline (agledger_verify agentKeys,
145
+ or --agent-keys on a verifier). Public keys only.
146
+ agledger_discover also shows the key.
107
147
 
108
148
  Delegation (optional): when the agent acts for a person or another party, an
109
149
  RFC 8693 token-exchange result token is sent as AGLedger-On-Behalf-Of on every
@@ -116,7 +156,7 @@ POST. It comes from the process, never from a tool argument.
116
156
 
117
157
  Tools:
118
158
  agledger_discover Returns API health, your identity, and available scopes
119
- agledger_api Make any AGLedger API call; the API guides you via nextSteps
159
+ agledger_api Make any AGLedger API call; most responses carry nextSteps
120
160
  agledger_verify Verify an audit export offline (hash chain + signatures)
121
161
 
122
162
  Exit codes: 0 clean, 1 runtime failure, 2 usage or configuration error.
@@ -128,6 +168,8 @@ Exit codes: 0 clean, 1 runtime failure, 2 usage or configuration error.
128
168
  const tokenCmd = process.env[OIDC_ENV.TOKEN_CMD] || undefined;
129
169
  const tokenFile = process.env[OIDC_ENV.TOKEN_FILE] || undefined;
130
170
  const agentId = process.env[OIDC_ENV.AGENT_ID] || undefined;
171
+ const agentKeysPath = process.env[OIDC_ENV.AGENT_KEYS_FILE];
172
+ const agentKeysFile = agentKeysPath ? resolvePath(agentKeysPath) : undefined;
131
173
  if (!apiKey && !tokenCmd && !tokenFile) {
132
174
  process.stderr.write('Error: no credential configured. Set one of:\n' +
133
175
  ' --api-key <key> or AGLEDGER_API_KEY an AGLedger API key\n' +
@@ -159,10 +201,16 @@ Exit codes: 0 clean, 1 runtime failure, 2 usage or configuration error.
159
201
  process.exit(EXIT_USAGE_ERROR);
160
202
  }
161
203
  }
162
- oidc = {
163
- getOidcToken: tokenCmd ? oidcTokenFromCommand(tokenCmd) : oidcTokenFromFile(tokenFile),
164
- origin: tokenCmd ? OIDC_ENV.TOKEN_CMD : OIDC_ENV.TOKEN_FILE,
204
+ // The checks above leave one of the two set; the command wins.
205
+ const source = tokenCmd
206
+ ? { getOidcToken: oidcTokenFromCommand(tokenCmd), origin: OIDC_ENV.TOKEN_CMD }
207
+ : tokenFile
208
+ ? { getOidcToken: oidcTokenFromFile(tokenFile), origin: OIDC_ENV.TOKEN_FILE }
209
+ : undefined;
210
+ oidc = source && {
211
+ ...source,
165
212
  ...(agentId ? { agentId } : {}),
213
+ ...(agentKeysFile ? { agentKeysFile } : {}),
166
214
  onWarning: (message) => process.stderr.write(`Warning: ${message}\n`),
167
215
  };
168
216
  }
@@ -176,9 +224,10 @@ Exit codes: 0 clean, 1 runtime failure, 2 usage or configuration error.
176
224
  'AGLedger is self-hosted, so the MCP server cannot guess your Server.\n');
177
225
  process.exit(EXIT_USAGE_ERROR);
178
226
  }
179
- // A URL that does not parse used to start the server, which then failed
180
- // every tool call; `localhost:3000` parses, with `localhost:` as its scheme.
181
- // Only an absolute http(s) URL can reach a Server.
227
+ // Refuse a URL that does not parse, or that parses with another scheme:
228
+ // `localhost:3000` parses, with `localhost:` as its scheme. Only an absolute
229
+ // http(s) URL can reach a Server, so starting with anything else would fail
230
+ // every tool call.
182
231
  const urlProblem = apiUrlProblem(apiUrl);
183
232
  if (urlProblem) {
184
233
  const origin = values['api-url'] !== undefined ? '--api-url' : 'AGLEDGER_API_URL';
@@ -232,7 +281,7 @@ Exit codes: 0 clean, 1 runtime failure, 2 usage or configuration error.
232
281
  process.stderr.write(zodSplitWarning(zod));
233
282
  const transport = new StdioServerTransport();
234
283
  server.mcp.connect(transport).catch((err) => {
235
- process.stderr.write(`Fatal: failed to start MCP server: ${err instanceof Error ? err.message : String(err)}\n`);
284
+ process.stderr.write(`Fatal: failed to start MCP server: ${fatalText(err)}\n`);
236
285
  process.exit(EXIT_RUNTIME_FAILURE);
237
286
  });
238
287
  }
package/dist/server.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
1
+ import { McpServer } from '@modelcontextprotocol/server';
2
2
  import { ApiClient } from './api-client.js';
3
3
  import { type OidcCertCredentialOptions } from './credentials.js';
4
4
  import { type DelegationSourceOptions } from './delegation.js';
@@ -24,6 +24,15 @@ export interface AgledgerMcpServerOptions {
24
24
  /** Request timeout in milliseconds. Default 30000. */
25
25
  timeoutMs?: number;
26
26
  }
27
+ /**
28
+ * What the export's entry count was held to. A chain cut from the end still
29
+ * verifies entry by entry, so the report names the stated count, if any, that
30
+ * stood behind the number of entries it verified.
31
+ */
32
+ export declare function entryCountLine(count: {
33
+ expectedEntries: number | null;
34
+ statedEntries: number | null;
35
+ }): string;
27
36
  /**
28
37
  * The summary lines on the evidence bodies an `?evidence=true` export
29
38
  * inlines, each re-bound to the hash its entry signs. Silent when every one
@@ -49,6 +58,9 @@ export declare function peerMessageLines(wire: {
49
58
  export declare class AgledgerMcpServer {
50
59
  readonly mcp: McpServer;
51
60
  readonly client: ApiClient;
61
+ /** The cert credential, when the server authenticates with an OIDC token source. */
62
+ private readonly oidcCredential;
63
+ private readonly agentKeysFile;
52
64
  constructor(options: AgledgerMcpServerOptions);
53
65
  private registerTools;
54
66
  private registerResources;