@orangecheck/agent-mcp 0.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/LICENSE +21 -0
- package/README.md +145 -0
- package/dist/index.d.mts +29 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +62 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +56 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +62 -0
- package/src/index.test.ts +112 -0
- package/src/index.ts +158 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 OrangeCheck
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# @orangecheck/agent-mcp
|
|
2
|
+
|
|
3
|
+
Wrap [Model Context Protocol](https://modelcontextprotocol.io/) tool invocations in [OC Agent](https://github.com/orangecheck/oc-agent-protocol) envelopes. Every call produces a signed, scope-constrained, verifiable `agent-action` stamp.
|
|
4
|
+
|
|
5
|
+
## Why
|
|
6
|
+
|
|
7
|
+
MCP servers expose tools to AI agents. The MCP wire protocol has no native notion of "who authorized this invocation" beyond whatever the transport layer carries. For anything consequential — sending email, moving sats, committing code, publishing content — the server (or any downstream auditor) should be able to verify:
|
|
8
|
+
|
|
9
|
+
1. **Which principal stands behind this agent.** A Bitcoin address, not an opaque issuer.
|
|
10
|
+
2. **What scope authorizes this specific call.** A declarative string like `mcp:invoke(server=…,tool=…)`.
|
|
11
|
+
3. **That the agent hasn't exceeded its grant.** Automatic via OC Agent's sub-scope algorithm.
|
|
12
|
+
4. **That the invocation happened before any revocation.** Optional OTS anchoring.
|
|
13
|
+
|
|
14
|
+
`@orangecheck/agent-mcp` canonicalizes the `(server, tool, arguments)` tuple, hashes it, and stamps the hash as an `agent-action`. The server sees a verifiable authority artifact alongside the call itself.
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm i @orangecheck/agent-mcp
|
|
20
|
+
# peer deps:
|
|
21
|
+
npm i @orangecheck/agent-core @orangecheck/agent-signer
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Quickstart
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { invokeWithStamp } from '@orangecheck/agent-mcp';
|
|
28
|
+
|
|
29
|
+
// You already have an agent SignerRef and a DelegationEnvelope from
|
|
30
|
+
// @orangecheck/agent-signer. The delegation grants something like
|
|
31
|
+
// mcp:invoke(server=https://mcp.example.com,tool=search)
|
|
32
|
+
|
|
33
|
+
const { result, action } = await invokeWithStamp({
|
|
34
|
+
agent,
|
|
35
|
+
delegation,
|
|
36
|
+
invocation: {
|
|
37
|
+
server: 'https://mcp.example.com',
|
|
38
|
+
tool: 'search',
|
|
39
|
+
arguments: { query: 'bitcoin identity', limit: 10 },
|
|
40
|
+
},
|
|
41
|
+
call: async (inv) => myMcpClient.invoke(inv.server, inv.tool, inv.arguments),
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// `result` is whatever the MCP server returned.
|
|
45
|
+
// `action` is the signed envelope; ship it alongside the result, publish to
|
|
46
|
+
// Nostr kind-30084, anchor to OpenTimestamps, etc.
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## What gets stamped
|
|
50
|
+
|
|
51
|
+
The stamp's `content.hash` is a SHA-256 of the RFC-8785-canonicalized JSON:
|
|
52
|
+
|
|
53
|
+
```json
|
|
54
|
+
{
|
|
55
|
+
"arguments": <canonical-json of invocation.arguments>,
|
|
56
|
+
"server": <invocation.server, trimmed>,
|
|
57
|
+
"tool": <invocation.tool>
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
This shape is minimal by design — a verifier reconstructs it from the three fields without knowing anything MCP-server-specific.
|
|
62
|
+
|
|
63
|
+
The action's `scope_exercised` defaults to `mcp:invoke(server=<server>,tool=<tool>)`, which is the tightest scope that exactly identifies the call. Callers who want a narrower or different scope can pass `scopeExercised` explicitly; `stampInvocation` pre-flights sub-scope containment against the delegation.
|
|
64
|
+
|
|
65
|
+
## API
|
|
66
|
+
|
|
67
|
+
### `canonicalizeInvocation(inv) -> Uint8Array`
|
|
68
|
+
|
|
69
|
+
RFC 8785 JSON canonicalization of `{server, tool, arguments}`. Byte-identical across implementations.
|
|
70
|
+
|
|
71
|
+
### `invocationHash(inv) -> "sha256:<64-hex>"`
|
|
72
|
+
|
|
73
|
+
SHA-256 of the canonical bytes, prefixed for the envelope's `content.hash`.
|
|
74
|
+
|
|
75
|
+
### `stampInvocation(input) -> Promise<ActionEnvelope>`
|
|
76
|
+
|
|
77
|
+
Produces a signed `agent-action` without calling the tool. Useful for pre-authorization flows or batch stamping.
|
|
78
|
+
|
|
79
|
+
### `invokeWithStamp(input) -> Promise<{ result, action }>`
|
|
80
|
+
|
|
81
|
+
Stamps the invocation first, then calls the tool. The stamp is returned alongside the tool's result — ship them together.
|
|
82
|
+
|
|
83
|
+
Rationale for stamping-before-calling: if the downstream call hangs or fails, there is still an on-record commitment that this agent attempted this specific invocation. The server-side verifier sees a stamped intent plus the server's own transcript.
|
|
84
|
+
|
|
85
|
+
## Server-side verification
|
|
86
|
+
|
|
87
|
+
A server receiving a stamped invocation verifies it with `@orangecheck/agent-core`:
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
import { verifyAction } from '@orangecheck/agent-core';
|
|
91
|
+
|
|
92
|
+
const r = await verifyAction({
|
|
93
|
+
action,
|
|
94
|
+
delegation,
|
|
95
|
+
verifyBip322: async (m, s, a) => bip322.verify(a, m, s),
|
|
96
|
+
});
|
|
97
|
+
if (!r.ok) throw new Error(`auth failed: ${r.code}`);
|
|
98
|
+
|
|
99
|
+
// Also re-hash the incoming (server, tool, arguments) to confirm the stamp
|
|
100
|
+
// covers exactly this request:
|
|
101
|
+
import { invocationHash } from '@orangecheck/agent-mcp';
|
|
102
|
+
const expectedHash = invocationHash({
|
|
103
|
+
server: myUrl,
|
|
104
|
+
tool: req.tool,
|
|
105
|
+
arguments: req.arguments,
|
|
106
|
+
});
|
|
107
|
+
if (expectedHash !== action.content.hash) {
|
|
108
|
+
throw new Error('stamp does not cover this request');
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Composing with OpenTimestamps
|
|
113
|
+
|
|
114
|
+
For invocations where priority-against-revocation matters:
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
import { stampCommit } from '@orangecheck/stamp-ots';
|
|
118
|
+
|
|
119
|
+
const { action } = await invokeWithStamp({ /* … */ });
|
|
120
|
+
action.ots = await stampCommit(action.id);
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## Composing with Nostr
|
|
124
|
+
|
|
125
|
+
Publish the action for public auditability (OC Agent reuses OC Stamp's kind-30084 transport, disambiguated by envelope `kind`):
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
await nostr.publish({
|
|
129
|
+
kind: 30084,
|
|
130
|
+
tags: [
|
|
131
|
+
['d', 'oc-agent-act:' + action.id],
|
|
132
|
+
['kind', 'agent-action'],
|
|
133
|
+
['delegation', action.delegation_id],
|
|
134
|
+
['agent', action.signer.address],
|
|
135
|
+
['scope', action.scope_exercised],
|
|
136
|
+
['hash', action.content.hash],
|
|
137
|
+
['signed_at', action.signed_at],
|
|
138
|
+
],
|
|
139
|
+
content: JSON.stringify(action),
|
|
140
|
+
});
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## License
|
|
144
|
+
|
|
145
|
+
MIT. See [LICENSE](./LICENSE).
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { DelegationEnvelope } from '@orangecheck/agent-core';
|
|
2
|
+
import { SignerRef, ActionEnvelope } from '@orangecheck/agent-signer';
|
|
3
|
+
|
|
4
|
+
interface McpInvocation {
|
|
5
|
+
server: string;
|
|
6
|
+
tool: string;
|
|
7
|
+
arguments: Record<string, unknown>;
|
|
8
|
+
}
|
|
9
|
+
declare function canonicalizeInvocation(inv: McpInvocation): Uint8Array;
|
|
10
|
+
declare function invocationHash(inv: McpInvocation): string;
|
|
11
|
+
declare function invocationId(agentAddress: string, signedAt: string, delegationId: string, scopeExercised: string, inv: McpInvocation): string;
|
|
12
|
+
interface StampInvocationInput {
|
|
13
|
+
agent: SignerRef;
|
|
14
|
+
delegation: DelegationEnvelope;
|
|
15
|
+
invocation: McpInvocation;
|
|
16
|
+
scopeExercised?: string;
|
|
17
|
+
signedAt?: Date;
|
|
18
|
+
}
|
|
19
|
+
declare function stampInvocation(input: StampInvocationInput): Promise<ActionEnvelope>;
|
|
20
|
+
interface InvokeWithStampInput<T> extends StampInvocationInput {
|
|
21
|
+
call: (inv: McpInvocation) => Promise<T>;
|
|
22
|
+
}
|
|
23
|
+
interface InvokeWithStampResult<T> {
|
|
24
|
+
result: T;
|
|
25
|
+
action: ActionEnvelope;
|
|
26
|
+
}
|
|
27
|
+
declare function invokeWithStamp<T>(input: InvokeWithStampInput<T>): Promise<InvokeWithStampResult<T>>;
|
|
28
|
+
|
|
29
|
+
export { type InvokeWithStampInput, type InvokeWithStampResult, type McpInvocation, type StampInvocationInput, canonicalizeInvocation, invocationHash, invocationId, invokeWithStamp, stampInvocation };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { DelegationEnvelope } from '@orangecheck/agent-core';
|
|
2
|
+
import { SignerRef, ActionEnvelope } from '@orangecheck/agent-signer';
|
|
3
|
+
|
|
4
|
+
interface McpInvocation {
|
|
5
|
+
server: string;
|
|
6
|
+
tool: string;
|
|
7
|
+
arguments: Record<string, unknown>;
|
|
8
|
+
}
|
|
9
|
+
declare function canonicalizeInvocation(inv: McpInvocation): Uint8Array;
|
|
10
|
+
declare function invocationHash(inv: McpInvocation): string;
|
|
11
|
+
declare function invocationId(agentAddress: string, signedAt: string, delegationId: string, scopeExercised: string, inv: McpInvocation): string;
|
|
12
|
+
interface StampInvocationInput {
|
|
13
|
+
agent: SignerRef;
|
|
14
|
+
delegation: DelegationEnvelope;
|
|
15
|
+
invocation: McpInvocation;
|
|
16
|
+
scopeExercised?: string;
|
|
17
|
+
signedAt?: Date;
|
|
18
|
+
}
|
|
19
|
+
declare function stampInvocation(input: StampInvocationInput): Promise<ActionEnvelope>;
|
|
20
|
+
interface InvokeWithStampInput<T> extends StampInvocationInput {
|
|
21
|
+
call: (inv: McpInvocation) => Promise<T>;
|
|
22
|
+
}
|
|
23
|
+
interface InvokeWithStampResult<T> {
|
|
24
|
+
result: T;
|
|
25
|
+
action: ActionEnvelope;
|
|
26
|
+
}
|
|
27
|
+
declare function invokeWithStamp<T>(input: InvokeWithStampInput<T>): Promise<InvokeWithStampResult<T>>;
|
|
28
|
+
|
|
29
|
+
export { type InvokeWithStampInput, type InvokeWithStampResult, type McpInvocation, type StampInvocationInput, canonicalizeInvocation, invocationHash, invocationId, invokeWithStamp, stampInvocation };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var sha256 = require('@noble/hashes/sha256');
|
|
4
|
+
var agentCore = require('@orangecheck/agent-core');
|
|
5
|
+
var agentSigner = require('@orangecheck/agent-signer');
|
|
6
|
+
|
|
7
|
+
// src/index.ts
|
|
8
|
+
function canonicalizeInvocation(inv) {
|
|
9
|
+
const canon = {
|
|
10
|
+
arguments: inv.arguments,
|
|
11
|
+
server: inv.server.trim(),
|
|
12
|
+
tool: inv.tool
|
|
13
|
+
};
|
|
14
|
+
const str = agentCore.canonicalize(canon);
|
|
15
|
+
return new TextEncoder().encode(str + "\n");
|
|
16
|
+
}
|
|
17
|
+
function invocationHash(inv) {
|
|
18
|
+
return "sha256:" + agentCore.hexEncode(sha256.sha256(canonicalizeInvocation(inv)));
|
|
19
|
+
}
|
|
20
|
+
function invocationId(agentAddress, signedAt, delegationId, scopeExercised, inv) {
|
|
21
|
+
return agentCore.computeActionId({
|
|
22
|
+
address: agentAddress,
|
|
23
|
+
content_hash: invocationHash(inv),
|
|
24
|
+
content_length: canonicalizeInvocation(inv).byteLength,
|
|
25
|
+
content_mime: "application/vnd.oc-agent.mcp-invocation+json",
|
|
26
|
+
signed_at: signedAt,
|
|
27
|
+
delegation_id: delegationId,
|
|
28
|
+
scope_exercised: scopeExercised
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
async function stampInvocation(input) {
|
|
32
|
+
const scopeExercised = input.scopeExercised ?? `mcp:invoke(server=${input.invocation.server.trim()},tool=${input.invocation.tool})`;
|
|
33
|
+
const granted = input.delegation.scopes.map(agentCore.parseScope);
|
|
34
|
+
const exercisedParsed = agentCore.parseScope(scopeExercised);
|
|
35
|
+
if (!granted.some((g) => agentCore.isSubScope(exercisedParsed, g))) {
|
|
36
|
+
throw new Error(
|
|
37
|
+
`stampInvocation: scope_exercised (${scopeExercised}) is not a sub-scope of any granted scope`
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
const bytes = canonicalizeInvocation(input.invocation);
|
|
41
|
+
return agentSigner.signAsAgent({
|
|
42
|
+
agent: input.agent,
|
|
43
|
+
delegation: input.delegation,
|
|
44
|
+
content: { hash: invocationHash(input.invocation), length: bytes.byteLength },
|
|
45
|
+
mime: "application/vnd.oc-agent.mcp-invocation+json",
|
|
46
|
+
scopeExercised,
|
|
47
|
+
signedAt: input.signedAt
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
async function invokeWithStamp(input) {
|
|
51
|
+
const action = await stampInvocation(input);
|
|
52
|
+
const result = await input.call(input.invocation);
|
|
53
|
+
return { result, action };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
exports.canonicalizeInvocation = canonicalizeInvocation;
|
|
57
|
+
exports.invocationHash = invocationHash;
|
|
58
|
+
exports.invocationId = invocationId;
|
|
59
|
+
exports.invokeWithStamp = invokeWithStamp;
|
|
60
|
+
exports.stampInvocation = stampInvocation;
|
|
61
|
+
//# sourceMappingURL=index.js.map
|
|
62
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["canonicalize","hexEncode","sha256","computeActionId","parseScope","isSubScope","signAsAgent"],"mappings":";;;;;;;AAqDO,SAAS,uBAAuB,GAAA,EAAgC;AACnE,EAAA,MAAM,KAAA,GAAQ;AAAA,IACV,WAAW,GAAA,CAAI,SAAA;AAAA,IACf,MAAA,EAAQ,GAAA,CAAI,MAAA,CAAO,IAAA,EAAK;AAAA,IACxB,MAAM,GAAA,CAAI;AAAA,GACd;AACA,EAAA,MAAM,GAAA,GAAMA,uBAAa,KAAsD,CAAA;AAC/E,EAAA,OAAO,IAAI,WAAA,EAAY,CAAE,MAAA,CAAO,MAAM,IAAI,CAAA;AAC9C;AAEO,SAAS,eAAe,GAAA,EAA4B;AACvD,EAAA,OAAO,YAAYC,mBAAA,CAAUC,aAAA,CAAO,sBAAA,CAAuB,GAAG,CAAC,CAAC,CAAA;AACpE;AAEO,SAAS,YAAA,CACZ,YAAA,EACA,QAAA,EACA,YAAA,EACA,gBACA,GAAA,EACM;AACN,EAAA,OAAOC,yBAAA,CAAgB;AAAA,IACnB,OAAA,EAAS,YAAA;AAAA,IACT,YAAA,EAAc,eAAe,GAAG,CAAA;AAAA,IAChC,cAAA,EAAgB,sBAAA,CAAuB,GAAG,CAAA,CAAE,UAAA;AAAA,IAC5C,YAAA,EAAc,8CAAA;AAAA,IACd,SAAA,EAAW,QAAA;AAAA,IACX,aAAA,EAAe,YAAA;AAAA,IACf,eAAA,EAAiB;AAAA,GACpB,CAAA;AACL;AAoBA,eAAsB,gBAAgB,KAAA,EAAsD;AACxF,EAAA,MAAM,cAAA,GACF,KAAA,CAAM,cAAA,IACN,CAAA,kBAAA,EAAqB,KAAA,CAAM,UAAA,CAAW,MAAA,CAAO,IAAA,EAAM,CAAA,MAAA,EAAS,KAAA,CAAM,UAAA,CAAW,IAAI,CAAA,CAAA,CAAA;AAKrF,EAAA,MAAM,OAAA,GAAU,KAAA,CAAM,UAAA,CAAW,MAAA,CAAO,IAAIC,oBAAU,CAAA;AACtD,EAAA,MAAM,eAAA,GAAkBA,qBAAW,cAAc,CAAA;AACjD,EAAA,IAAI,CAAC,QAAQ,IAAA,CAAK,CAAC,MAAMC,oBAAA,CAAW,eAAA,EAAiB,CAAC,CAAC,CAAA,EAAG;AACtD,IAAA,MAAM,IAAI,KAAA;AAAA,MACN,qCAAqC,cAAc,CAAA,yCAAA;AAAA,KACvD;AAAA,EACJ;AAEA,EAAA,MAAM,KAAA,GAAQ,sBAAA,CAAuB,KAAA,CAAM,UAAU,CAAA;AACrD,EAAA,OAAOC,uBAAA,CAAY;AAAA,IACf,OAAO,KAAA,CAAM,KAAA;AAAA,IACb,YAAY,KAAA,CAAM,UAAA;AAAA,IAClB,OAAA,EAAS,EAAE,IAAA,EAAM,cAAA,CAAe,MAAM,UAAU,CAAA,EAAG,MAAA,EAAQ,KAAA,CAAM,UAAA,EAAW;AAAA,IAC5E,IAAA,EAAM,8CAAA;AAAA,IACN,cAAA;AAAA,IACA,UAAU,KAAA,CAAM;AAAA,GACnB,CAAA;AACL;AAyBA,eAAsB,gBAAmB,KAAA,EAAmE;AACxG,EAAA,MAAM,MAAA,GAAS,MAAM,eAAA,CAAgB,KAAK,CAAA;AAC1C,EAAA,MAAM,MAAA,GAAS,MAAM,KAAA,CAAM,IAAA,CAAK,MAAM,UAAU,CAAA;AAChD,EAAA,OAAO,EAAE,QAAQ,MAAA,EAAO;AAC5B","file":"index.js","sourcesContent":["// @orangecheck/agent-mcp — wrap MCP tool invocations in OC Agent envelopes.\n//\n// An MCP client calls tools on an MCP server: { server, name, arguments }.\n// This module stamps each invocation as an `agent-action` whose `content.hash`\n// is a SHA-256 of the canonicalized (server, name, arguments) tuple, scoped\n// as `mcp:invoke(server=<url>,tool=<name>)`. The server — or any counterparty\n// — verifies the action before trusting the call.\n//\n// The wrapper is transport-agnostic. Wire it into whatever MCP client you use\n// by calling `invokeWithStamp()` around the raw call.\n\nimport { sha256 } from '@noble/hashes/sha256';\nimport {\n canonicalize,\n computeActionId,\n hexEncode,\n isSubScope,\n parseScope,\n type DelegationEnvelope,\n} from '@orangecheck/agent-core';\nimport {\n signAsAgent,\n type ActionEnvelope,\n type SignerRef,\n} from '@orangecheck/agent-signer';\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Canonicalization of an MCP invocation\n// ─────────────────────────────────────────────────────────────────────────────\n\nexport interface McpInvocation {\n /** Origin URL of the MCP server. Trimmed and lowercased for canonicalization. */\n server: string;\n /** Tool name (method) being invoked. */\n tool: string;\n /** JSON-serializable arguments. */\n arguments: Record<string, unknown>;\n}\n\n/**\n * Produce the canonical bytes for an MCP invocation. The bytes are what the\n * action envelope's `content.hash` is a SHA-256 of.\n *\n * Shape:\n * {\n * \"arguments\": <canonical JSON of args>,\n * \"server\": <server url>,\n * \"tool\": <tool name>\n * }\n *\n * The shape is intentionally minimal so verifiers can reconstruct it without\n * knowing anything server- or tool-specific.\n */\nexport function canonicalizeInvocation(inv: McpInvocation): Uint8Array {\n const canon = {\n arguments: inv.arguments,\n server: inv.server.trim(),\n tool: inv.tool,\n };\n const str = canonicalize(canon as unknown as Parameters<typeof canonicalize>[0]);\n return new TextEncoder().encode(str + '\\n');\n}\n\nexport function invocationHash(inv: McpInvocation): string {\n return 'sha256:' + hexEncode(sha256(canonicalizeInvocation(inv)));\n}\n\nexport function invocationId(\n agentAddress: string,\n signedAt: string,\n delegationId: string,\n scopeExercised: string,\n inv: McpInvocation\n): string {\n return computeActionId({\n address: agentAddress,\n content_hash: invocationHash(inv),\n content_length: canonicalizeInvocation(inv).byteLength,\n content_mime: 'application/vnd.oc-agent.mcp-invocation+json',\n signed_at: signedAt,\n delegation_id: delegationId,\n scope_exercised: scopeExercised,\n });\n}\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Stamped invocation\n// ─────────────────────────────────────────────────────────────────────────────\n\nexport interface StampInvocationInput {\n agent: SignerRef;\n delegation: DelegationEnvelope;\n invocation: McpInvocation;\n /** Defaults to `mcp:invoke(server=<server>,tool=<tool>)` — the tightest admissible sub-scope. */\n scopeExercised?: string;\n signedAt?: Date;\n}\n\n/**\n * Stamp an MCP invocation without actually calling it. Useful when the caller\n * wants the signed envelope for out-of-band auditing before (or independent of)\n * running the tool.\n */\nexport async function stampInvocation(input: StampInvocationInput): Promise<ActionEnvelope> {\n const scopeExercised =\n input.scopeExercised ??\n `mcp:invoke(server=${input.invocation.server.trim()},tool=${input.invocation.tool})`;\n\n // Tiny pre-flight: confirm the exercised scope is a sub-scope of *some*\n // granted scope. This keeps a common integration mistake from producing\n // an envelope that later fails verifier-side.\n const granted = input.delegation.scopes.map(parseScope);\n const exercisedParsed = parseScope(scopeExercised);\n if (!granted.some((g) => isSubScope(exercisedParsed, g))) {\n throw new Error(\n `stampInvocation: scope_exercised (${scopeExercised}) is not a sub-scope of any granted scope`\n );\n }\n\n const bytes = canonicalizeInvocation(input.invocation);\n return signAsAgent({\n agent: input.agent,\n delegation: input.delegation,\n content: { hash: invocationHash(input.invocation), length: bytes.byteLength },\n mime: 'application/vnd.oc-agent.mcp-invocation+json',\n scopeExercised,\n signedAt: input.signedAt,\n });\n}\n\n// ─────────────────────────────────────────────────────────────────────────────\n// invokeWithStamp — call the tool AND produce the stamp around it\n// ─────────────────────────────────────────────────────────────────────────────\n\nexport interface InvokeWithStampInput<T> extends StampInvocationInput {\n /** The underlying transport call. Returns whatever the MCP server returns. */\n call: (inv: McpInvocation) => Promise<T>;\n}\n\nexport interface InvokeWithStampResult<T> {\n result: T;\n action: ActionEnvelope;\n}\n\n/**\n * Stamp the invocation *first*, then execute the call. The stamped envelope\n * is returned alongside the result so the caller can ship them together.\n *\n * Rationale for stamping first: if the downstream call hangs or fails, we\n * still have an on-record commitment that this agent attempted this specific\n * invocation. The server-side verifier sees a stamped intent plus the server's\n * own transcript.\n */\nexport async function invokeWithStamp<T>(input: InvokeWithStampInput<T>): Promise<InvokeWithStampResult<T>> {\n const action = await stampInvocation(input);\n const result = await input.call(input.invocation);\n return { result, action };\n}\n"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { sha256 } from '@noble/hashes/sha256';
|
|
2
|
+
import { canonicalize, hexEncode, computeActionId, parseScope, isSubScope } from '@orangecheck/agent-core';
|
|
3
|
+
import { signAsAgent } from '@orangecheck/agent-signer';
|
|
4
|
+
|
|
5
|
+
// src/index.ts
|
|
6
|
+
function canonicalizeInvocation(inv) {
|
|
7
|
+
const canon = {
|
|
8
|
+
arguments: inv.arguments,
|
|
9
|
+
server: inv.server.trim(),
|
|
10
|
+
tool: inv.tool
|
|
11
|
+
};
|
|
12
|
+
const str = canonicalize(canon);
|
|
13
|
+
return new TextEncoder().encode(str + "\n");
|
|
14
|
+
}
|
|
15
|
+
function invocationHash(inv) {
|
|
16
|
+
return "sha256:" + hexEncode(sha256(canonicalizeInvocation(inv)));
|
|
17
|
+
}
|
|
18
|
+
function invocationId(agentAddress, signedAt, delegationId, scopeExercised, inv) {
|
|
19
|
+
return computeActionId({
|
|
20
|
+
address: agentAddress,
|
|
21
|
+
content_hash: invocationHash(inv),
|
|
22
|
+
content_length: canonicalizeInvocation(inv).byteLength,
|
|
23
|
+
content_mime: "application/vnd.oc-agent.mcp-invocation+json",
|
|
24
|
+
signed_at: signedAt,
|
|
25
|
+
delegation_id: delegationId,
|
|
26
|
+
scope_exercised: scopeExercised
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
async function stampInvocation(input) {
|
|
30
|
+
const scopeExercised = input.scopeExercised ?? `mcp:invoke(server=${input.invocation.server.trim()},tool=${input.invocation.tool})`;
|
|
31
|
+
const granted = input.delegation.scopes.map(parseScope);
|
|
32
|
+
const exercisedParsed = parseScope(scopeExercised);
|
|
33
|
+
if (!granted.some((g) => isSubScope(exercisedParsed, g))) {
|
|
34
|
+
throw new Error(
|
|
35
|
+
`stampInvocation: scope_exercised (${scopeExercised}) is not a sub-scope of any granted scope`
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
const bytes = canonicalizeInvocation(input.invocation);
|
|
39
|
+
return signAsAgent({
|
|
40
|
+
agent: input.agent,
|
|
41
|
+
delegation: input.delegation,
|
|
42
|
+
content: { hash: invocationHash(input.invocation), length: bytes.byteLength },
|
|
43
|
+
mime: "application/vnd.oc-agent.mcp-invocation+json",
|
|
44
|
+
scopeExercised,
|
|
45
|
+
signedAt: input.signedAt
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
async function invokeWithStamp(input) {
|
|
49
|
+
const action = await stampInvocation(input);
|
|
50
|
+
const result = await input.call(input.invocation);
|
|
51
|
+
return { result, action };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export { canonicalizeInvocation, invocationHash, invocationId, invokeWithStamp, stampInvocation };
|
|
55
|
+
//# sourceMappingURL=index.mjs.map
|
|
56
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;;;AAqDO,SAAS,uBAAuB,GAAA,EAAgC;AACnE,EAAA,MAAM,KAAA,GAAQ;AAAA,IACV,WAAW,GAAA,CAAI,SAAA;AAAA,IACf,MAAA,EAAQ,GAAA,CAAI,MAAA,CAAO,IAAA,EAAK;AAAA,IACxB,MAAM,GAAA,CAAI;AAAA,GACd;AACA,EAAA,MAAM,GAAA,GAAM,aAAa,KAAsD,CAAA;AAC/E,EAAA,OAAO,IAAI,WAAA,EAAY,CAAE,MAAA,CAAO,MAAM,IAAI,CAAA;AAC9C;AAEO,SAAS,eAAe,GAAA,EAA4B;AACvD,EAAA,OAAO,YAAY,SAAA,CAAU,MAAA,CAAO,sBAAA,CAAuB,GAAG,CAAC,CAAC,CAAA;AACpE;AAEO,SAAS,YAAA,CACZ,YAAA,EACA,QAAA,EACA,YAAA,EACA,gBACA,GAAA,EACM;AACN,EAAA,OAAO,eAAA,CAAgB;AAAA,IACnB,OAAA,EAAS,YAAA;AAAA,IACT,YAAA,EAAc,eAAe,GAAG,CAAA;AAAA,IAChC,cAAA,EAAgB,sBAAA,CAAuB,GAAG,CAAA,CAAE,UAAA;AAAA,IAC5C,YAAA,EAAc,8CAAA;AAAA,IACd,SAAA,EAAW,QAAA;AAAA,IACX,aAAA,EAAe,YAAA;AAAA,IACf,eAAA,EAAiB;AAAA,GACpB,CAAA;AACL;AAoBA,eAAsB,gBAAgB,KAAA,EAAsD;AACxF,EAAA,MAAM,cAAA,GACF,KAAA,CAAM,cAAA,IACN,CAAA,kBAAA,EAAqB,KAAA,CAAM,UAAA,CAAW,MAAA,CAAO,IAAA,EAAM,CAAA,MAAA,EAAS,KAAA,CAAM,UAAA,CAAW,IAAI,CAAA,CAAA,CAAA;AAKrF,EAAA,MAAM,OAAA,GAAU,KAAA,CAAM,UAAA,CAAW,MAAA,CAAO,IAAI,UAAU,CAAA;AACtD,EAAA,MAAM,eAAA,GAAkB,WAAW,cAAc,CAAA;AACjD,EAAA,IAAI,CAAC,QAAQ,IAAA,CAAK,CAAC,MAAM,UAAA,CAAW,eAAA,EAAiB,CAAC,CAAC,CAAA,EAAG;AACtD,IAAA,MAAM,IAAI,KAAA;AAAA,MACN,qCAAqC,cAAc,CAAA,yCAAA;AAAA,KACvD;AAAA,EACJ;AAEA,EAAA,MAAM,KAAA,GAAQ,sBAAA,CAAuB,KAAA,CAAM,UAAU,CAAA;AACrD,EAAA,OAAO,WAAA,CAAY;AAAA,IACf,OAAO,KAAA,CAAM,KAAA;AAAA,IACb,YAAY,KAAA,CAAM,UAAA;AAAA,IAClB,OAAA,EAAS,EAAE,IAAA,EAAM,cAAA,CAAe,MAAM,UAAU,CAAA,EAAG,MAAA,EAAQ,KAAA,CAAM,UAAA,EAAW;AAAA,IAC5E,IAAA,EAAM,8CAAA;AAAA,IACN,cAAA;AAAA,IACA,UAAU,KAAA,CAAM;AAAA,GACnB,CAAA;AACL;AAyBA,eAAsB,gBAAmB,KAAA,EAAmE;AACxG,EAAA,MAAM,MAAA,GAAS,MAAM,eAAA,CAAgB,KAAK,CAAA;AAC1C,EAAA,MAAM,MAAA,GAAS,MAAM,KAAA,CAAM,IAAA,CAAK,MAAM,UAAU,CAAA;AAChD,EAAA,OAAO,EAAE,QAAQ,MAAA,EAAO;AAC5B","file":"index.mjs","sourcesContent":["// @orangecheck/agent-mcp — wrap MCP tool invocations in OC Agent envelopes.\n//\n// An MCP client calls tools on an MCP server: { server, name, arguments }.\n// This module stamps each invocation as an `agent-action` whose `content.hash`\n// is a SHA-256 of the canonicalized (server, name, arguments) tuple, scoped\n// as `mcp:invoke(server=<url>,tool=<name>)`. The server — or any counterparty\n// — verifies the action before trusting the call.\n//\n// The wrapper is transport-agnostic. Wire it into whatever MCP client you use\n// by calling `invokeWithStamp()` around the raw call.\n\nimport { sha256 } from '@noble/hashes/sha256';\nimport {\n canonicalize,\n computeActionId,\n hexEncode,\n isSubScope,\n parseScope,\n type DelegationEnvelope,\n} from '@orangecheck/agent-core';\nimport {\n signAsAgent,\n type ActionEnvelope,\n type SignerRef,\n} from '@orangecheck/agent-signer';\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Canonicalization of an MCP invocation\n// ─────────────────────────────────────────────────────────────────────────────\n\nexport interface McpInvocation {\n /** Origin URL of the MCP server. Trimmed and lowercased for canonicalization. */\n server: string;\n /** Tool name (method) being invoked. */\n tool: string;\n /** JSON-serializable arguments. */\n arguments: Record<string, unknown>;\n}\n\n/**\n * Produce the canonical bytes for an MCP invocation. The bytes are what the\n * action envelope's `content.hash` is a SHA-256 of.\n *\n * Shape:\n * {\n * \"arguments\": <canonical JSON of args>,\n * \"server\": <server url>,\n * \"tool\": <tool name>\n * }\n *\n * The shape is intentionally minimal so verifiers can reconstruct it without\n * knowing anything server- or tool-specific.\n */\nexport function canonicalizeInvocation(inv: McpInvocation): Uint8Array {\n const canon = {\n arguments: inv.arguments,\n server: inv.server.trim(),\n tool: inv.tool,\n };\n const str = canonicalize(canon as unknown as Parameters<typeof canonicalize>[0]);\n return new TextEncoder().encode(str + '\\n');\n}\n\nexport function invocationHash(inv: McpInvocation): string {\n return 'sha256:' + hexEncode(sha256(canonicalizeInvocation(inv)));\n}\n\nexport function invocationId(\n agentAddress: string,\n signedAt: string,\n delegationId: string,\n scopeExercised: string,\n inv: McpInvocation\n): string {\n return computeActionId({\n address: agentAddress,\n content_hash: invocationHash(inv),\n content_length: canonicalizeInvocation(inv).byteLength,\n content_mime: 'application/vnd.oc-agent.mcp-invocation+json',\n signed_at: signedAt,\n delegation_id: delegationId,\n scope_exercised: scopeExercised,\n });\n}\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Stamped invocation\n// ─────────────────────────────────────────────────────────────────────────────\n\nexport interface StampInvocationInput {\n agent: SignerRef;\n delegation: DelegationEnvelope;\n invocation: McpInvocation;\n /** Defaults to `mcp:invoke(server=<server>,tool=<tool>)` — the tightest admissible sub-scope. */\n scopeExercised?: string;\n signedAt?: Date;\n}\n\n/**\n * Stamp an MCP invocation without actually calling it. Useful when the caller\n * wants the signed envelope for out-of-band auditing before (or independent of)\n * running the tool.\n */\nexport async function stampInvocation(input: StampInvocationInput): Promise<ActionEnvelope> {\n const scopeExercised =\n input.scopeExercised ??\n `mcp:invoke(server=${input.invocation.server.trim()},tool=${input.invocation.tool})`;\n\n // Tiny pre-flight: confirm the exercised scope is a sub-scope of *some*\n // granted scope. This keeps a common integration mistake from producing\n // an envelope that later fails verifier-side.\n const granted = input.delegation.scopes.map(parseScope);\n const exercisedParsed = parseScope(scopeExercised);\n if (!granted.some((g) => isSubScope(exercisedParsed, g))) {\n throw new Error(\n `stampInvocation: scope_exercised (${scopeExercised}) is not a sub-scope of any granted scope`\n );\n }\n\n const bytes = canonicalizeInvocation(input.invocation);\n return signAsAgent({\n agent: input.agent,\n delegation: input.delegation,\n content: { hash: invocationHash(input.invocation), length: bytes.byteLength },\n mime: 'application/vnd.oc-agent.mcp-invocation+json',\n scopeExercised,\n signedAt: input.signedAt,\n });\n}\n\n// ─────────────────────────────────────────────────────────────────────────────\n// invokeWithStamp — call the tool AND produce the stamp around it\n// ─────────────────────────────────────────────────────────────────────────────\n\nexport interface InvokeWithStampInput<T> extends StampInvocationInput {\n /** The underlying transport call. Returns whatever the MCP server returns. */\n call: (inv: McpInvocation) => Promise<T>;\n}\n\nexport interface InvokeWithStampResult<T> {\n result: T;\n action: ActionEnvelope;\n}\n\n/**\n * Stamp the invocation *first*, then execute the call. The stamped envelope\n * is returned alongside the result so the caller can ship them together.\n *\n * Rationale for stamping first: if the downstream call hangs or fails, we\n * still have an on-record commitment that this agent attempted this specific\n * invocation. The server-side verifier sees a stamped intent plus the server's\n * own transcript.\n */\nexport async function invokeWithStamp<T>(input: InvokeWithStampInput<T>): Promise<InvokeWithStampResult<T>> {\n const action = await stampInvocation(input);\n const result = await input.call(input.invocation);\n return { result, action };\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@orangecheck/agent-mcp",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Wrap Model Context Protocol tool invocations in OC Agent envelopes — every call produces a signed, scope-constrained, verifiable agent-action stamp.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"bitcoin",
|
|
7
|
+
"agent",
|
|
8
|
+
"authority",
|
|
9
|
+
"mcp",
|
|
10
|
+
"model-context-protocol",
|
|
11
|
+
"bip322",
|
|
12
|
+
"oc-agent",
|
|
13
|
+
"orangecheck"
|
|
14
|
+
],
|
|
15
|
+
"author": "OrangeCheck",
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://github.com/orangecheck/oc-packages.git",
|
|
20
|
+
"directory": "agent-mcp"
|
|
21
|
+
},
|
|
22
|
+
"homepage": "https://github.com/orangecheck/oc-agent-protocol",
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/orangecheck/oc-agent-protocol/issues"
|
|
25
|
+
},
|
|
26
|
+
"main": "./dist/index.js",
|
|
27
|
+
"module": "./dist/index.mjs",
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"import": "./dist/index.mjs",
|
|
33
|
+
"require": "./dist/index.js"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist",
|
|
38
|
+
"src",
|
|
39
|
+
"README.md",
|
|
40
|
+
"LICENSE"
|
|
41
|
+
],
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsup",
|
|
44
|
+
"dev": "tsup --watch",
|
|
45
|
+
"type-check": "tsc --noEmit",
|
|
46
|
+
"test": "vitest run",
|
|
47
|
+
"test:watch": "vitest",
|
|
48
|
+
"clean": "rm -rf dist",
|
|
49
|
+
"prepublishOnly": "npm run clean && npm run build"
|
|
50
|
+
},
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"@noble/hashes": "^1.5.0",
|
|
53
|
+
"@orangecheck/agent-core": "^0.1.0",
|
|
54
|
+
"@orangecheck/agent-signer": "^0.1.0"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"@types/node": "^22.10.2",
|
|
58
|
+
"tsup": "^8.3.5",
|
|
59
|
+
"typescript": "^5.7.2",
|
|
60
|
+
"vitest": "^3.2.4"
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { verifyAction } from '@orangecheck/agent-core';
|
|
4
|
+
import { createDelegation } from '@orangecheck/agent-signer';
|
|
5
|
+
|
|
6
|
+
import { canonicalizeInvocation, invocationHash, invokeWithStamp, stampInvocation } from './index.js';
|
|
7
|
+
|
|
8
|
+
const PRINCIPAL = 'bc1qprincipal000000000000000000000000000000';
|
|
9
|
+
const AGENT = 'bc1qagent0000000000000000000000000000000000';
|
|
10
|
+
|
|
11
|
+
const fakeSign = (address: string) => async (msg: string) =>
|
|
12
|
+
Buffer.from(`sig:${address}:${msg}`, 'utf8').toString('base64');
|
|
13
|
+
|
|
14
|
+
const fakeVerify = async (msg: string, sig: string, address: string) =>
|
|
15
|
+
sig === Buffer.from(`sig:${address}:${msg}`, 'utf8').toString('base64');
|
|
16
|
+
|
|
17
|
+
describe('canonicalizeInvocation', () => {
|
|
18
|
+
it('produces byte-identical output for identical inputs', () => {
|
|
19
|
+
const a = canonicalizeInvocation({
|
|
20
|
+
server: 'https://mcp.example.com',
|
|
21
|
+
tool: 'search',
|
|
22
|
+
arguments: { q: 'bitcoin', limit: 10 },
|
|
23
|
+
});
|
|
24
|
+
const b = canonicalizeInvocation({
|
|
25
|
+
server: 'https://mcp.example.com',
|
|
26
|
+
tool: 'search',
|
|
27
|
+
arguments: { limit: 10, q: 'bitcoin' }, // keys reordered
|
|
28
|
+
});
|
|
29
|
+
expect(a).toEqual(b);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('produces different hashes for different argument values', () => {
|
|
33
|
+
const h1 = invocationHash({
|
|
34
|
+
server: 'https://mcp.example.com',
|
|
35
|
+
tool: 'search',
|
|
36
|
+
arguments: { q: 'a' },
|
|
37
|
+
});
|
|
38
|
+
const h2 = invocationHash({
|
|
39
|
+
server: 'https://mcp.example.com',
|
|
40
|
+
tool: 'search',
|
|
41
|
+
arguments: { q: 'b' },
|
|
42
|
+
});
|
|
43
|
+
expect(h1).not.toBe(h2);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
describe('stampInvocation', () => {
|
|
48
|
+
it('produces an action that verifies against its delegation', async () => {
|
|
49
|
+
const delegation = await createDelegation({
|
|
50
|
+
principal: { address: PRINCIPAL, signMessage: fakeSign(PRINCIPAL) },
|
|
51
|
+
agentAddress: AGENT,
|
|
52
|
+
scopes: ['mcp:invoke(server=https://mcp.example.com,tool=search)'],
|
|
53
|
+
ttlMs: 60 * 60 * 1000,
|
|
54
|
+
});
|
|
55
|
+
const action = await stampInvocation({
|
|
56
|
+
agent: { address: AGENT, signMessage: fakeSign(AGENT) },
|
|
57
|
+
delegation,
|
|
58
|
+
invocation: {
|
|
59
|
+
server: 'https://mcp.example.com',
|
|
60
|
+
tool: 'search',
|
|
61
|
+
arguments: { q: 'bitcoin' },
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
expect(action.content.mime).toBe('application/vnd.oc-agent.mcp-invocation+json');
|
|
65
|
+
const r = await verifyAction({ action, delegation, verifyBip322: fakeVerify });
|
|
66
|
+
expect(r.ok).toBe(true);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('rejects when exercised scope is not a sub-scope of granted', async () => {
|
|
70
|
+
const delegation = await createDelegation({
|
|
71
|
+
principal: { address: PRINCIPAL, signMessage: fakeSign(PRINCIPAL) },
|
|
72
|
+
agentAddress: AGENT,
|
|
73
|
+
scopes: ['mcp:invoke(server=https://mcp.example.com,tool=search)'],
|
|
74
|
+
ttlMs: 60 * 60 * 1000,
|
|
75
|
+
});
|
|
76
|
+
await expect(
|
|
77
|
+
stampInvocation({
|
|
78
|
+
agent: { address: AGENT, signMessage: fakeSign(AGENT) },
|
|
79
|
+
delegation,
|
|
80
|
+
invocation: {
|
|
81
|
+
server: 'https://mcp.example.com',
|
|
82
|
+
tool: 'send_email',
|
|
83
|
+
arguments: {},
|
|
84
|
+
},
|
|
85
|
+
})
|
|
86
|
+
).rejects.toThrow(/not a sub-scope/);
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
describe('invokeWithStamp', () => {
|
|
91
|
+
it('stamps first then calls', async () => {
|
|
92
|
+
const delegation = await createDelegation({
|
|
93
|
+
principal: { address: PRINCIPAL, signMessage: fakeSign(PRINCIPAL) },
|
|
94
|
+
agentAddress: AGENT,
|
|
95
|
+
scopes: ['mcp:invoke(server=https://mcp.example.com,tool=search)'],
|
|
96
|
+
ttlMs: 60 * 60 * 1000,
|
|
97
|
+
});
|
|
98
|
+
const { result, action } = await invokeWithStamp({
|
|
99
|
+
agent: { address: AGENT, signMessage: fakeSign(AGENT) },
|
|
100
|
+
delegation,
|
|
101
|
+
invocation: {
|
|
102
|
+
server: 'https://mcp.example.com',
|
|
103
|
+
tool: 'search',
|
|
104
|
+
arguments: { q: 'bitcoin' },
|
|
105
|
+
},
|
|
106
|
+
call: async () => ({ hits: 42 }),
|
|
107
|
+
});
|
|
108
|
+
expect(result).toEqual({ hits: 42 });
|
|
109
|
+
expect(action.kind).toBe('agent-action');
|
|
110
|
+
expect(action.delegation_id).toBe(delegation.id);
|
|
111
|
+
});
|
|
112
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// @orangecheck/agent-mcp — wrap MCP tool invocations in OC Agent envelopes.
|
|
2
|
+
//
|
|
3
|
+
// An MCP client calls tools on an MCP server: { server, name, arguments }.
|
|
4
|
+
// This module stamps each invocation as an `agent-action` whose `content.hash`
|
|
5
|
+
// is a SHA-256 of the canonicalized (server, name, arguments) tuple, scoped
|
|
6
|
+
// as `mcp:invoke(server=<url>,tool=<name>)`. The server — or any counterparty
|
|
7
|
+
// — verifies the action before trusting the call.
|
|
8
|
+
//
|
|
9
|
+
// The wrapper is transport-agnostic. Wire it into whatever MCP client you use
|
|
10
|
+
// by calling `invokeWithStamp()` around the raw call.
|
|
11
|
+
|
|
12
|
+
import { sha256 } from '@noble/hashes/sha256';
|
|
13
|
+
import {
|
|
14
|
+
canonicalize,
|
|
15
|
+
computeActionId,
|
|
16
|
+
hexEncode,
|
|
17
|
+
isSubScope,
|
|
18
|
+
parseScope,
|
|
19
|
+
type DelegationEnvelope,
|
|
20
|
+
} from '@orangecheck/agent-core';
|
|
21
|
+
import {
|
|
22
|
+
signAsAgent,
|
|
23
|
+
type ActionEnvelope,
|
|
24
|
+
type SignerRef,
|
|
25
|
+
} from '@orangecheck/agent-signer';
|
|
26
|
+
|
|
27
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
28
|
+
// Canonicalization of an MCP invocation
|
|
29
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
30
|
+
|
|
31
|
+
export interface McpInvocation {
|
|
32
|
+
/** Origin URL of the MCP server. Trimmed and lowercased for canonicalization. */
|
|
33
|
+
server: string;
|
|
34
|
+
/** Tool name (method) being invoked. */
|
|
35
|
+
tool: string;
|
|
36
|
+
/** JSON-serializable arguments. */
|
|
37
|
+
arguments: Record<string, unknown>;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Produce the canonical bytes for an MCP invocation. The bytes are what the
|
|
42
|
+
* action envelope's `content.hash` is a SHA-256 of.
|
|
43
|
+
*
|
|
44
|
+
* Shape:
|
|
45
|
+
* {
|
|
46
|
+
* "arguments": <canonical JSON of args>,
|
|
47
|
+
* "server": <server url>,
|
|
48
|
+
* "tool": <tool name>
|
|
49
|
+
* }
|
|
50
|
+
*
|
|
51
|
+
* The shape is intentionally minimal so verifiers can reconstruct it without
|
|
52
|
+
* knowing anything server- or tool-specific.
|
|
53
|
+
*/
|
|
54
|
+
export function canonicalizeInvocation(inv: McpInvocation): Uint8Array {
|
|
55
|
+
const canon = {
|
|
56
|
+
arguments: inv.arguments,
|
|
57
|
+
server: inv.server.trim(),
|
|
58
|
+
tool: inv.tool,
|
|
59
|
+
};
|
|
60
|
+
const str = canonicalize(canon as unknown as Parameters<typeof canonicalize>[0]);
|
|
61
|
+
return new TextEncoder().encode(str + '\n');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function invocationHash(inv: McpInvocation): string {
|
|
65
|
+
return 'sha256:' + hexEncode(sha256(canonicalizeInvocation(inv)));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function invocationId(
|
|
69
|
+
agentAddress: string,
|
|
70
|
+
signedAt: string,
|
|
71
|
+
delegationId: string,
|
|
72
|
+
scopeExercised: string,
|
|
73
|
+
inv: McpInvocation
|
|
74
|
+
): string {
|
|
75
|
+
return computeActionId({
|
|
76
|
+
address: agentAddress,
|
|
77
|
+
content_hash: invocationHash(inv),
|
|
78
|
+
content_length: canonicalizeInvocation(inv).byteLength,
|
|
79
|
+
content_mime: 'application/vnd.oc-agent.mcp-invocation+json',
|
|
80
|
+
signed_at: signedAt,
|
|
81
|
+
delegation_id: delegationId,
|
|
82
|
+
scope_exercised: scopeExercised,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
87
|
+
// Stamped invocation
|
|
88
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
89
|
+
|
|
90
|
+
export interface StampInvocationInput {
|
|
91
|
+
agent: SignerRef;
|
|
92
|
+
delegation: DelegationEnvelope;
|
|
93
|
+
invocation: McpInvocation;
|
|
94
|
+
/** Defaults to `mcp:invoke(server=<server>,tool=<tool>)` — the tightest admissible sub-scope. */
|
|
95
|
+
scopeExercised?: string;
|
|
96
|
+
signedAt?: Date;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Stamp an MCP invocation without actually calling it. Useful when the caller
|
|
101
|
+
* wants the signed envelope for out-of-band auditing before (or independent of)
|
|
102
|
+
* running the tool.
|
|
103
|
+
*/
|
|
104
|
+
export async function stampInvocation(input: StampInvocationInput): Promise<ActionEnvelope> {
|
|
105
|
+
const scopeExercised =
|
|
106
|
+
input.scopeExercised ??
|
|
107
|
+
`mcp:invoke(server=${input.invocation.server.trim()},tool=${input.invocation.tool})`;
|
|
108
|
+
|
|
109
|
+
// Tiny pre-flight: confirm the exercised scope is a sub-scope of *some*
|
|
110
|
+
// granted scope. This keeps a common integration mistake from producing
|
|
111
|
+
// an envelope that later fails verifier-side.
|
|
112
|
+
const granted = input.delegation.scopes.map(parseScope);
|
|
113
|
+
const exercisedParsed = parseScope(scopeExercised);
|
|
114
|
+
if (!granted.some((g) => isSubScope(exercisedParsed, g))) {
|
|
115
|
+
throw new Error(
|
|
116
|
+
`stampInvocation: scope_exercised (${scopeExercised}) is not a sub-scope of any granted scope`
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const bytes = canonicalizeInvocation(input.invocation);
|
|
121
|
+
return signAsAgent({
|
|
122
|
+
agent: input.agent,
|
|
123
|
+
delegation: input.delegation,
|
|
124
|
+
content: { hash: invocationHash(input.invocation), length: bytes.byteLength },
|
|
125
|
+
mime: 'application/vnd.oc-agent.mcp-invocation+json',
|
|
126
|
+
scopeExercised,
|
|
127
|
+
signedAt: input.signedAt,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
132
|
+
// invokeWithStamp — call the tool AND produce the stamp around it
|
|
133
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
134
|
+
|
|
135
|
+
export interface InvokeWithStampInput<T> extends StampInvocationInput {
|
|
136
|
+
/** The underlying transport call. Returns whatever the MCP server returns. */
|
|
137
|
+
call: (inv: McpInvocation) => Promise<T>;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export interface InvokeWithStampResult<T> {
|
|
141
|
+
result: T;
|
|
142
|
+
action: ActionEnvelope;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Stamp the invocation *first*, then execute the call. The stamped envelope
|
|
147
|
+
* is returned alongside the result so the caller can ship them together.
|
|
148
|
+
*
|
|
149
|
+
* Rationale for stamping first: if the downstream call hangs or fails, we
|
|
150
|
+
* still have an on-record commitment that this agent attempted this specific
|
|
151
|
+
* invocation. The server-side verifier sees a stamped intent plus the server's
|
|
152
|
+
* own transcript.
|
|
153
|
+
*/
|
|
154
|
+
export async function invokeWithStamp<T>(input: InvokeWithStampInput<T>): Promise<InvokeWithStampResult<T>> {
|
|
155
|
+
const action = await stampInvocation(input);
|
|
156
|
+
const result = await input.call(input.invocation);
|
|
157
|
+
return { result, action };
|
|
158
|
+
}
|