@openagentid/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/CHANGELOG.md +49 -0
- package/LICENSE-APACHE +100 -0
- package/LICENSE-MIT +21 -0
- package/README.md +168 -0
- package/dist/client.cjs +91 -0
- package/dist/client.cjs.map +1 -0
- package/dist/client.d.cts +82 -0
- package/dist/client.d.ts +82 -0
- package/dist/client.js +88 -0
- package/dist/client.js.map +1 -0
- package/dist/errors.cjs +105 -0
- package/dist/errors.cjs.map +1 -0
- package/dist/errors.d.cts +64 -0
- package/dist/errors.d.ts +64 -0
- package/dist/errors.js +98 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.cjs +524 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +507 -0
- package/dist/index.js.map +1 -0
- package/dist/server.cjs +327 -0
- package/dist/server.cjs.map +1 -0
- package/dist/server.d.cts +66 -0
- package/dist/server.d.ts +66 -0
- package/dist/server.js +324 -0
- package/dist/server.js.map +1 -0
- package/dist/skills.cjs +96 -0
- package/dist/skills.cjs.map +1 -0
- package/dist/skills.d.cts +87 -0
- package/dist/skills.d.ts +87 -0
- package/dist/skills.js +91 -0
- package/dist/skills.js.map +1 -0
- package/dist/types.cjs +10 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.cts +179 -0
- package/dist/types.d.ts +179 -0
- package/dist/types.js +7 -0
- package/dist/types.js.map +1 -0
- package/package.json +102 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `@openagent/mcp` are documented in this file.
|
|
4
|
+
|
|
5
|
+
The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
### Changed
|
|
11
|
+
|
|
12
|
+
- **Breaking:** package renamed from `@openagent/mcp` to `@openagentid/mcp`. The `@openagent` npm scope is squatted by a third party; the project now publishes under the `@openagentid` scope, matching the GitHub org OpenAgentID. Consumers must update dependency names and imports. Effective with the first npm publish under the new scope.
|
|
13
|
+
|
|
14
|
+
## [0.1.0] - 2026-04-11
|
|
15
|
+
|
|
16
|
+
### Added
|
|
17
|
+
|
|
18
|
+
- `withOpenAgent(server, config)` — Proxy-based middleware that wraps any
|
|
19
|
+
`@modelcontextprotocol/sdk` `McpServer` instance and runs every tool
|
|
20
|
+
call through an OpenAgent identity verification + scope enforcement
|
|
21
|
+
pipeline. Intercepts both `tool()` (deprecated) and `registerTool()`
|
|
22
|
+
registration paths.
|
|
23
|
+
- `withOpenAgentClient(client, config)` — Client-side interceptor that
|
|
24
|
+
signs outbound `callTool` requests with the caller's DID and proof
|
|
25
|
+
(Arsenal ACT or challenge response), injecting an
|
|
26
|
+
`_meta.openagent.identity` envelope. Forwards all other client
|
|
27
|
+
methods unchanged.
|
|
28
|
+
- `OpenAgentMcpConfig` — `agent`, `requireScopes`, `requireIdentity`,
|
|
29
|
+
`skillsPolicy`, `preCall`, `postCall`, `onError`, `scopeFormat`.
|
|
30
|
+
- `createSkillsPolicy(options)` — Composable hook for SKILLS.md-style
|
|
31
|
+
policy enforcement on skill-like tool calls. Default matchers:
|
|
32
|
+
`invoke_skill`, `run_skill`, `execute_skill`, `skill.invoke`,
|
|
33
|
+
`skill.run`.
|
|
34
|
+
- `InMemorySkillsPolicyStore` — Reference implementation of the
|
|
35
|
+
`SkillsPolicyStore` interface, suitable for tests and small
|
|
36
|
+
deployments.
|
|
37
|
+
- Typed errors: `OpenAgentMcpError`, `MissingIdentityError`,
|
|
38
|
+
`IdentityVerificationError`, `AuthorizationDeniedError`,
|
|
39
|
+
`SkillsPolicyDeniedError`. Each carries a JSON-RPC error code in the
|
|
40
|
+
`-32000`..`-32099` server-error range.
|
|
41
|
+
- Audit metadata stamping — successful tool results carry an
|
|
42
|
+
`audit_id`, `verified_did`, and `scopes` block under
|
|
43
|
+
`_meta.openagent` so callers can correlate logs end-to-end.
|
|
44
|
+
- Vitest test suite — unit tests against a synthetic server and
|
|
45
|
+
integration tests against a real `@modelcontextprotocol/sdk`
|
|
46
|
+
`McpServer` connected via `InMemoryTransport`.
|
|
47
|
+
- Built and published against `@modelcontextprotocol/sdk@^1.27.0`.
|
|
48
|
+
- Documentation: README quickstart, hooks reference, errors table,
|
|
49
|
+
configuration reference, links to <https://openagent.id/docs/integrations/mcp>.
|
package/LICENSE-APACHE
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity.
|
|
18
|
+
|
|
19
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
20
|
+
exercising permissions granted by this License.
|
|
21
|
+
|
|
22
|
+
"Source" form shall mean the preferred form for making modifications.
|
|
23
|
+
|
|
24
|
+
"Object" form shall mean any form resulting from mechanical
|
|
25
|
+
transformation or translation of a Source form.
|
|
26
|
+
|
|
27
|
+
"Work" shall mean the work of authorship made available under the License.
|
|
28
|
+
|
|
29
|
+
"Contribution" shall mean any work of authorship submitted to the Licensor
|
|
30
|
+
for inclusion in the Work.
|
|
31
|
+
|
|
32
|
+
"Contributor" shall mean Licensor and any Legal Entity on behalf of whom
|
|
33
|
+
a Contribution has been received by the Licensor.
|
|
34
|
+
|
|
35
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
36
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
37
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
38
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
39
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
40
|
+
Work and such Derivative Works in Source or Object form.
|
|
41
|
+
|
|
42
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
43
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
44
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
45
|
+
patent license to make, have made, use, offer to sell, sell,
|
|
46
|
+
import, and otherwise transfer the Work.
|
|
47
|
+
|
|
48
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
49
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
50
|
+
modifications, and in Source or Object form, provided that You
|
|
51
|
+
meet the following conditions:
|
|
52
|
+
|
|
53
|
+
(a) You must give any other recipients of the Work or
|
|
54
|
+
Derivative Works a copy of this License; and
|
|
55
|
+
|
|
56
|
+
(b) You must cause any modified files to carry prominent notices
|
|
57
|
+
stating that You changed the files; and
|
|
58
|
+
|
|
59
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
60
|
+
that You distribute, all copyright, patent, trademark, and
|
|
61
|
+
attribution notices from the Source form of the Work; and
|
|
62
|
+
|
|
63
|
+
(d) If the Work includes a "NOTICE" text file, You must include
|
|
64
|
+
a readable copy of the attribution notices contained within
|
|
65
|
+
such NOTICE file.
|
|
66
|
+
|
|
67
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
68
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
69
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
70
|
+
this License, without any additional terms or conditions.
|
|
71
|
+
|
|
72
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
73
|
+
names, trademarks, service marks, or product names of the Licensor.
|
|
74
|
+
|
|
75
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
76
|
+
agreed to in writing, Licensor provides the Work on an "AS IS" BASIS,
|
|
77
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
|
|
78
|
+
|
|
79
|
+
8. Limitation of Liability. In no event shall any Contributor be liable
|
|
80
|
+
for any damages arising from the use of the Work.
|
|
81
|
+
|
|
82
|
+
9. Accepting Warranty or Additional Liability. You may act as a
|
|
83
|
+
commercial distributor and offer support, warranty, or indemnity
|
|
84
|
+
obligations for a fee.
|
|
85
|
+
|
|
86
|
+
END OF TERMS AND CONDITIONS
|
|
87
|
+
|
|
88
|
+
Copyright 2026 L1fe AI
|
|
89
|
+
|
|
90
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
91
|
+
you may not use this file except in compliance with the License.
|
|
92
|
+
You may obtain a copy of the License at
|
|
93
|
+
|
|
94
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
95
|
+
|
|
96
|
+
Unless required by applicable law or agreed to in writing, software
|
|
97
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
98
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
99
|
+
See the License for the specific language governing permissions and
|
|
100
|
+
limitations under the License.
|
package/LICENSE-MIT
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 L1fe AI
|
|
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,168 @@
|
|
|
1
|
+
# @openagentid/mcp
|
|
2
|
+
|
|
3
|
+
OpenAgent middleware for the [Model Context Protocol](https://modelcontextprotocol.io).
|
|
4
|
+
|
|
5
|
+
Add OAS identity verification, Arsenal capability tokens, and AEGIS authorization to any MCP server in three lines.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @openagentid/mcp @modelcontextprotocol/sdk @openagentid/sdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Server: add auth to MCP in 3 lines
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
17
|
+
import { withOpenAgent } from '@openagentid/mcp';
|
|
18
|
+
import { agent } from './my-agent.js';
|
|
19
|
+
|
|
20
|
+
const server = new McpServer({ name: 'my-tools', version: '0.1.0' });
|
|
21
|
+
|
|
22
|
+
const authedServer = withOpenAgent(server, {
|
|
23
|
+
agent,
|
|
24
|
+
requireScopes: (toolName) => [`mcp:${toolName}:invoke`],
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
authedServer.registerTool(
|
|
28
|
+
'search_web',
|
|
29
|
+
{ description: 'search the web', inputSchema: { q: z.string() } },
|
|
30
|
+
async ({ q }) => ({ content: [{ type: 'text', text: `results for ${q}` }] }),
|
|
31
|
+
);
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
That is it. Every tool call now:
|
|
35
|
+
|
|
36
|
+
1. Pulls the identity envelope from `extra._meta.openagent.identity`.
|
|
37
|
+
2. Verifies it against your `IdentityVerifier` (OAS DID + Arsenal ACT + AEGIS policy).
|
|
38
|
+
3. Enforces the per-tool scopes returned by `requireScopes`.
|
|
39
|
+
4. Runs the optional `skillsPolicy` hook (consults SKILLS.md-style rules).
|
|
40
|
+
5. Runs the user's tool body.
|
|
41
|
+
6. Stamps `audit_id`, `verified_did`, and `scopes` into the response `_meta.openagent`.
|
|
42
|
+
|
|
43
|
+
## Client: sign every outbound call
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
47
|
+
import { withOpenAgentClient } from '@openagentid/mcp';
|
|
48
|
+
import { agent } from './my-agent.js';
|
|
49
|
+
|
|
50
|
+
const client = new Client({ name: 'my-agent', version: '0.1.0' });
|
|
51
|
+
const authedClient = withOpenAgentClient(client, {
|
|
52
|
+
agent,
|
|
53
|
+
audience: 'did:oas:vendor:tools-server',
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
await authedClient.callTool({
|
|
57
|
+
name: 'search_web',
|
|
58
|
+
arguments: { q: 'mcp' },
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
The interceptor wraps `callTool` only — `listTools`, `listResources`, `getPrompt`, and every other client method pass straight through.
|
|
63
|
+
|
|
64
|
+
## Hooks
|
|
65
|
+
|
|
66
|
+
`withOpenAgent` accepts three optional hooks plus the skills policy:
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
withOpenAgent(server, {
|
|
70
|
+
agent,
|
|
71
|
+
preCall: ({ toolName, identity }) => {
|
|
72
|
+
metrics.increment('mcp.tool.calls', { tool: toolName, did: identity.did });
|
|
73
|
+
},
|
|
74
|
+
postCall: ({ toolName, identity, durationMs, result }) => {
|
|
75
|
+
audit.write({ tool: toolName, did: identity.did, ms: durationMs });
|
|
76
|
+
},
|
|
77
|
+
onError: ({ toolName, identity, error }) => {
|
|
78
|
+
audit.writeError({ tool: toolName, did: identity?.did ?? null, error });
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Skills policy
|
|
84
|
+
|
|
85
|
+
MCP tools and SKILLS.md entries describe the same thing — a capability an
|
|
86
|
+
agent can invoke. When an MCP server exposes a skill-like tool (default
|
|
87
|
+
matchers: `invoke_skill`, `run_skill`, `execute_skill`, `skill.invoke`,
|
|
88
|
+
`skill.run`), the middleware consults a policy hook before allowing the
|
|
89
|
+
call.
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
import {
|
|
93
|
+
withOpenAgent,
|
|
94
|
+
createSkillsPolicy,
|
|
95
|
+
InMemorySkillsPolicyStore,
|
|
96
|
+
} from '@openagentid/mcp';
|
|
97
|
+
|
|
98
|
+
const skillsPolicy = createSkillsPolicy({
|
|
99
|
+
store: new InMemorySkillsPolicyStore([
|
|
100
|
+
{ skillName: 'cook_dinner', dids: ['did:oas:org:kitchen:agent:alice'] },
|
|
101
|
+
{ skillName: 'order_pizza', dids: null }, // any verified caller
|
|
102
|
+
]),
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
withOpenAgent(server, { agent, skillsPolicy });
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
You can also write your own store that reads from `SKILLS.md` files, a
|
|
109
|
+
remote service, or AEGIS policy bundles — just implement the
|
|
110
|
+
`SkillsPolicyStore` interface.
|
|
111
|
+
|
|
112
|
+
## Errors
|
|
113
|
+
|
|
114
|
+
Every middleware-rejected call surfaces a typed error:
|
|
115
|
+
|
|
116
|
+
| Error | Code | When |
|
|
117
|
+
| ------------------------------ | -------- | ----------------------------------------------- |
|
|
118
|
+
| `MissingIdentityError` | `-32001` | `_meta.openagent.identity` was absent |
|
|
119
|
+
| `IdentityVerificationError` | `-32001` | The verifier rejected the envelope |
|
|
120
|
+
| `AuthorizationDeniedError` | `-32002` | Caller does not hold the required scopes |
|
|
121
|
+
| `SkillsPolicyDeniedError` | `-32003` | The skills policy hook returned `allow: false` |
|
|
122
|
+
|
|
123
|
+
All extend `OpenAgentMcpError`, which carries a `code`, `message`, and
|
|
124
|
+
optional `data` payload — ready for serialization through the JSON-RPC
|
|
125
|
+
error envelope the MCP SDK expects.
|
|
126
|
+
|
|
127
|
+
## Configuration reference
|
|
128
|
+
|
|
129
|
+
```ts
|
|
130
|
+
interface OpenAgentMcpConfig {
|
|
131
|
+
agent: Agent;
|
|
132
|
+
requireScopes?: (toolName: string, args: unknown) => readonly string[];
|
|
133
|
+
requireIdentity?: boolean; // default: true
|
|
134
|
+
skillsPolicy?: SkillsPolicyHook;
|
|
135
|
+
preCall?: PreCallHook;
|
|
136
|
+
postCall?: PostCallHook;
|
|
137
|
+
onError?: ErrorHook;
|
|
138
|
+
scopeFormat?: (toolName: string) => string;
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Versioning
|
|
143
|
+
|
|
144
|
+
This package is built against `@modelcontextprotocol/sdk@^1.27.0`. Both
|
|
145
|
+
the deprecated `tool(name, schema, cb)` overloads and the current
|
|
146
|
+
`registerTool(name, config, cb)` API are intercepted, so any 1.x server
|
|
147
|
+
works without code changes.
|
|
148
|
+
|
|
149
|
+
## Testing
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
npm test
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
The test suite uses [vitest](https://vitest.dev) and exercises the
|
|
156
|
+
middleware against both a synthetic MCP server and a real
|
|
157
|
+
`@modelcontextprotocol/sdk` server connected over the SDK's
|
|
158
|
+
`InMemoryTransport`.
|
|
159
|
+
|
|
160
|
+
## Documentation
|
|
161
|
+
|
|
162
|
+
- Spec: <https://openagent.id/docs/integrations/mcp>
|
|
163
|
+
- OpenAgent SDK: <https://openagent.id/docs/sdk>
|
|
164
|
+
- MCP: <https://modelcontextprotocol.io>
|
|
165
|
+
|
|
166
|
+
## License
|
|
167
|
+
|
|
168
|
+
Apache-2.0
|
package/dist/client.cjs
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/client.ts
|
|
4
|
+
function withOpenAgentClient(client, config) {
|
|
5
|
+
if (config === null || typeof config !== "object") {
|
|
6
|
+
throw new TypeError("OpenAgentClientConfig must be an object");
|
|
7
|
+
}
|
|
8
|
+
if (config.agent === void 0 || config.agent === null) {
|
|
9
|
+
throw new TypeError("OpenAgentClientConfig.agent is required");
|
|
10
|
+
}
|
|
11
|
+
if (typeof config.agent.signRequest !== "function") {
|
|
12
|
+
throw new TypeError("OpenAgentClientConfig.agent.signRequest is required");
|
|
13
|
+
}
|
|
14
|
+
const interceptor = new ClientInterceptor(config);
|
|
15
|
+
return new Proxy(client, {
|
|
16
|
+
get(target, property, receiver) {
|
|
17
|
+
const value = Reflect.get(target, property, receiver);
|
|
18
|
+
if (typeof value !== "function") {
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
if (property === "callTool") {
|
|
22
|
+
return interceptor.wrapCallTool(
|
|
23
|
+
value,
|
|
24
|
+
target
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
return value.bind(target);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
var ClientInterceptor = class {
|
|
32
|
+
agent;
|
|
33
|
+
audience;
|
|
34
|
+
beforeCall;
|
|
35
|
+
constructor(config) {
|
|
36
|
+
this.agent = config.agent;
|
|
37
|
+
this.audience = config.audience;
|
|
38
|
+
this.beforeCall = config.beforeCall;
|
|
39
|
+
}
|
|
40
|
+
wrapCallTool(original, thisArg) {
|
|
41
|
+
const interceptor = this;
|
|
42
|
+
return async function wrappedCallTool(params, ...rest) {
|
|
43
|
+
const signed = await interceptor.signParams(params);
|
|
44
|
+
let toSend = signed;
|
|
45
|
+
if (interceptor.beforeCall !== void 0) {
|
|
46
|
+
const replacement = await Promise.resolve(
|
|
47
|
+
interceptor.beforeCall(signed)
|
|
48
|
+
);
|
|
49
|
+
if (replacement !== void 0) {
|
|
50
|
+
toSend = {
|
|
51
|
+
...replacement,
|
|
52
|
+
_meta: replacement._meta ?? signed._meta
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return original.call(thisArg, toSend, ...rest);
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
async signParams(params) {
|
|
60
|
+
const identity = await this.agent.signRequest(params.name, this.audience);
|
|
61
|
+
const existingMeta = params._meta ?? {};
|
|
62
|
+
const existingOpenAgent = typeof existingMeta["openagent"] === "object" && existingMeta["openagent"] !== null ? existingMeta["openagent"] : {};
|
|
63
|
+
return {
|
|
64
|
+
...params,
|
|
65
|
+
_meta: {
|
|
66
|
+
...existingMeta,
|
|
67
|
+
openagent: {
|
|
68
|
+
...existingOpenAgent,
|
|
69
|
+
identity
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
function buildIdentityMeta(identity) {
|
|
76
|
+
return {
|
|
77
|
+
openagent: {
|
|
78
|
+
identity: {
|
|
79
|
+
did: identity.did,
|
|
80
|
+
proof: identity.proof,
|
|
81
|
+
...identity.nonce !== void 0 ? { nonce: identity.nonce } : {},
|
|
82
|
+
...identity.context !== void 0 ? { context: identity.context } : {}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
exports.buildIdentityMeta = buildIdentityMeta;
|
|
89
|
+
exports.withOpenAgentClient = withOpenAgentClient;
|
|
90
|
+
//# sourceMappingURL=client.cjs.map
|
|
91
|
+
//# sourceMappingURL=client.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/client.ts"],"names":[],"mappings":"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","file":"client.cjs","sourcesContent":["/**\n * Client-side interceptor for @openagentid/mcp.\n *\n * Adds an OpenAgent identity envelope (`_meta.openagent.identity`) to\n * outbound MCP `tools/call` requests so an authenticated server can\n * verify the caller. The interceptor wraps the MCP client's `callTool`\n * method via a JavaScript Proxy — the rest of the client surface (list\n * tools, list resources, prompts) is forwarded unchanged.\n *\n * Usage:\n *\n * ```ts\n * import { Client } from '@modelcontextprotocol/sdk/client/index.js';\n * import { withOpenAgentClient } from '@openagentid/mcp/client';\n *\n * const client = new Client({ name: 'my-agent', version: '0.1.0' });\n * const authedClient = withOpenAgentClient(client, { agent });\n *\n * await authedClient.callTool({ name: 'search_web', arguments: { q: 'mcp' } });\n * ```\n */\n\nimport { Agent } from './types.js';\n\n/**\n * Minimal MCP client surface used by the interceptor. Like the server\n * adapter, we describe just the methods we need to keep peer-dep\n * compatibility broad.\n */\nexport interface McpClientLike {\n callTool(\n params: {\n name: string;\n arguments?: Record<string, unknown>;\n _meta?: Record<string, unknown>;\n },\n ...rest: unknown[]\n ): Promise<unknown>;\n}\n\nexport interface OpenAgentClientConfig {\n /** The agent doing the calling — used to sign each request. */\n agent: Agent;\n /**\n * Optional audience DID. When set, it is passed to\n * {@link Agent.signRequest} so the proof can be bound to a specific\n * server. Most production deployments should set this.\n */\n audience?: string;\n /**\n * Optional hook fired before every outbound call. Use it for client-\n * side metrics or to mutate the params (e.g., add tracing headers).\n * Returning a value replaces the params.\n */\n beforeCall?: (params: {\n name: string;\n arguments?: Record<string, unknown>;\n _meta?: Record<string, unknown>;\n }) => Promise<\n | { name: string; arguments?: Record<string, unknown>; _meta?: Record<string, unknown> }\n | void\n > | { name: string; arguments?: Record<string, unknown>; _meta?: Record<string, unknown> }\n | void;\n}\n\n/**\n * Wrap an MCP client so every outbound `callTool` carries an OpenAgent\n * identity envelope.\n */\nexport function withOpenAgentClient<T extends McpClientLike>(\n client: T,\n config: OpenAgentClientConfig,\n): T {\n if (config === null || typeof config !== 'object') {\n throw new TypeError('OpenAgentClientConfig must be an object');\n }\n if (config.agent === undefined || config.agent === null) {\n throw new TypeError('OpenAgentClientConfig.agent is required');\n }\n if (typeof config.agent.signRequest !== 'function') {\n throw new TypeError('OpenAgentClientConfig.agent.signRequest is required');\n }\n\n const interceptor = new ClientInterceptor(config);\n\n return new Proxy(client, {\n get(target, property, receiver) {\n const value = Reflect.get(target, property, receiver);\n if (typeof value !== 'function') {\n return value;\n }\n if (property === 'callTool') {\n return interceptor.wrapCallTool(\n value as McpClientLike['callTool'],\n target,\n );\n }\n return value.bind(target);\n },\n }) as T;\n}\n\nclass ClientInterceptor {\n private readonly agent: Agent;\n private readonly audience: string | undefined;\n private readonly beforeCall: OpenAgentClientConfig['beforeCall'];\n\n constructor(config: OpenAgentClientConfig) {\n this.agent = config.agent;\n this.audience = config.audience;\n this.beforeCall = config.beforeCall;\n }\n\n wrapCallTool(\n original: McpClientLike['callTool'],\n thisArg: unknown,\n ): McpClientLike['callTool'] {\n const interceptor = this;\n return async function wrappedCallTool(\n this: unknown,\n params: {\n name: string;\n arguments?: Record<string, unknown>;\n _meta?: Record<string, unknown>;\n },\n ...rest: unknown[]\n ): Promise<unknown> {\n const signed = await interceptor.signParams(params);\n let toSend: {\n name: string;\n arguments?: Record<string, unknown>;\n _meta: Record<string, unknown>;\n } = signed;\n if (interceptor.beforeCall !== undefined) {\n const replacement = await Promise.resolve(\n interceptor.beforeCall(signed),\n );\n if (replacement !== undefined) {\n // Re-attach the signed _meta if the hook stripped it.\n toSend = {\n ...replacement,\n _meta: replacement._meta ?? signed._meta,\n };\n }\n }\n return original.call(thisArg, toSend, ...rest);\n };\n }\n\n private async signParams(params: {\n name: string;\n arguments?: Record<string, unknown>;\n _meta?: Record<string, unknown>;\n }): Promise<{\n name: string;\n arguments?: Record<string, unknown>;\n _meta: Record<string, unknown>;\n }> {\n const identity = await this.agent.signRequest(params.name, this.audience);\n const existingMeta = params._meta ?? {};\n const existingOpenAgent =\n typeof existingMeta['openagent'] === 'object' &&\n existingMeta['openagent'] !== null\n ? (existingMeta['openagent'] as Record<string, unknown>)\n : {};\n return {\n ...params,\n _meta: {\n ...existingMeta,\n openagent: {\n ...existingOpenAgent,\n identity,\n },\n },\n };\n }\n}\n\n/**\n * Helper for tests and tools that want to construct the identity\n * envelope manually without going through a full Agent. Returns a `_meta`\n * object that can be merged into a CallToolRequest's params.\n */\nexport function buildIdentityMeta(identity: {\n did: string;\n proof: string;\n nonce?: string;\n context?: Record<string, unknown>;\n}): Record<string, unknown> {\n return {\n openagent: {\n identity: {\n did: identity.did,\n proof: identity.proof,\n ...(identity.nonce !== undefined ? { nonce: identity.nonce } : {}),\n ...(identity.context !== undefined\n ? { context: identity.context }\n : {}),\n },\n },\n };\n}\n"]}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { Agent } from './types.cjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Client-side interceptor for @openagentid/mcp.
|
|
5
|
+
*
|
|
6
|
+
* Adds an OpenAgent identity envelope (`_meta.openagent.identity`) to
|
|
7
|
+
* outbound MCP `tools/call` requests so an authenticated server can
|
|
8
|
+
* verify the caller. The interceptor wraps the MCP client's `callTool`
|
|
9
|
+
* method via a JavaScript Proxy — the rest of the client surface (list
|
|
10
|
+
* tools, list resources, prompts) is forwarded unchanged.
|
|
11
|
+
*
|
|
12
|
+
* Usage:
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
16
|
+
* import { withOpenAgentClient } from '@openagentid/mcp/client';
|
|
17
|
+
*
|
|
18
|
+
* const client = new Client({ name: 'my-agent', version: '0.1.0' });
|
|
19
|
+
* const authedClient = withOpenAgentClient(client, { agent });
|
|
20
|
+
*
|
|
21
|
+
* await authedClient.callTool({ name: 'search_web', arguments: { q: 'mcp' } });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Minimal MCP client surface used by the interceptor. Like the server
|
|
27
|
+
* adapter, we describe just the methods we need to keep peer-dep
|
|
28
|
+
* compatibility broad.
|
|
29
|
+
*/
|
|
30
|
+
interface McpClientLike {
|
|
31
|
+
callTool(params: {
|
|
32
|
+
name: string;
|
|
33
|
+
arguments?: Record<string, unknown>;
|
|
34
|
+
_meta?: Record<string, unknown>;
|
|
35
|
+
}, ...rest: unknown[]): Promise<unknown>;
|
|
36
|
+
}
|
|
37
|
+
interface OpenAgentClientConfig {
|
|
38
|
+
/** The agent doing the calling — used to sign each request. */
|
|
39
|
+
agent: Agent;
|
|
40
|
+
/**
|
|
41
|
+
* Optional audience DID. When set, it is passed to
|
|
42
|
+
* {@link Agent.signRequest} so the proof can be bound to a specific
|
|
43
|
+
* server. Most production deployments should set this.
|
|
44
|
+
*/
|
|
45
|
+
audience?: string;
|
|
46
|
+
/**
|
|
47
|
+
* Optional hook fired before every outbound call. Use it for client-
|
|
48
|
+
* side metrics or to mutate the params (e.g., add tracing headers).
|
|
49
|
+
* Returning a value replaces the params.
|
|
50
|
+
*/
|
|
51
|
+
beforeCall?: (params: {
|
|
52
|
+
name: string;
|
|
53
|
+
arguments?: Record<string, unknown>;
|
|
54
|
+
_meta?: Record<string, unknown>;
|
|
55
|
+
}) => Promise<{
|
|
56
|
+
name: string;
|
|
57
|
+
arguments?: Record<string, unknown>;
|
|
58
|
+
_meta?: Record<string, unknown>;
|
|
59
|
+
} | void> | {
|
|
60
|
+
name: string;
|
|
61
|
+
arguments?: Record<string, unknown>;
|
|
62
|
+
_meta?: Record<string, unknown>;
|
|
63
|
+
} | void;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Wrap an MCP client so every outbound `callTool` carries an OpenAgent
|
|
67
|
+
* identity envelope.
|
|
68
|
+
*/
|
|
69
|
+
declare function withOpenAgentClient<T extends McpClientLike>(client: T, config: OpenAgentClientConfig): T;
|
|
70
|
+
/**
|
|
71
|
+
* Helper for tests and tools that want to construct the identity
|
|
72
|
+
* envelope manually without going through a full Agent. Returns a `_meta`
|
|
73
|
+
* object that can be merged into a CallToolRequest's params.
|
|
74
|
+
*/
|
|
75
|
+
declare function buildIdentityMeta(identity: {
|
|
76
|
+
did: string;
|
|
77
|
+
proof: string;
|
|
78
|
+
nonce?: string;
|
|
79
|
+
context?: Record<string, unknown>;
|
|
80
|
+
}): Record<string, unknown>;
|
|
81
|
+
|
|
82
|
+
export { type McpClientLike, type OpenAgentClientConfig, buildIdentityMeta, withOpenAgentClient };
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { Agent } from './types.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Client-side interceptor for @openagentid/mcp.
|
|
5
|
+
*
|
|
6
|
+
* Adds an OpenAgent identity envelope (`_meta.openagent.identity`) to
|
|
7
|
+
* outbound MCP `tools/call` requests so an authenticated server can
|
|
8
|
+
* verify the caller. The interceptor wraps the MCP client's `callTool`
|
|
9
|
+
* method via a JavaScript Proxy — the rest of the client surface (list
|
|
10
|
+
* tools, list resources, prompts) is forwarded unchanged.
|
|
11
|
+
*
|
|
12
|
+
* Usage:
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
16
|
+
* import { withOpenAgentClient } from '@openagentid/mcp/client';
|
|
17
|
+
*
|
|
18
|
+
* const client = new Client({ name: 'my-agent', version: '0.1.0' });
|
|
19
|
+
* const authedClient = withOpenAgentClient(client, { agent });
|
|
20
|
+
*
|
|
21
|
+
* await authedClient.callTool({ name: 'search_web', arguments: { q: 'mcp' } });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Minimal MCP client surface used by the interceptor. Like the server
|
|
27
|
+
* adapter, we describe just the methods we need to keep peer-dep
|
|
28
|
+
* compatibility broad.
|
|
29
|
+
*/
|
|
30
|
+
interface McpClientLike {
|
|
31
|
+
callTool(params: {
|
|
32
|
+
name: string;
|
|
33
|
+
arguments?: Record<string, unknown>;
|
|
34
|
+
_meta?: Record<string, unknown>;
|
|
35
|
+
}, ...rest: unknown[]): Promise<unknown>;
|
|
36
|
+
}
|
|
37
|
+
interface OpenAgentClientConfig {
|
|
38
|
+
/** The agent doing the calling — used to sign each request. */
|
|
39
|
+
agent: Agent;
|
|
40
|
+
/**
|
|
41
|
+
* Optional audience DID. When set, it is passed to
|
|
42
|
+
* {@link Agent.signRequest} so the proof can be bound to a specific
|
|
43
|
+
* server. Most production deployments should set this.
|
|
44
|
+
*/
|
|
45
|
+
audience?: string;
|
|
46
|
+
/**
|
|
47
|
+
* Optional hook fired before every outbound call. Use it for client-
|
|
48
|
+
* side metrics or to mutate the params (e.g., add tracing headers).
|
|
49
|
+
* Returning a value replaces the params.
|
|
50
|
+
*/
|
|
51
|
+
beforeCall?: (params: {
|
|
52
|
+
name: string;
|
|
53
|
+
arguments?: Record<string, unknown>;
|
|
54
|
+
_meta?: Record<string, unknown>;
|
|
55
|
+
}) => Promise<{
|
|
56
|
+
name: string;
|
|
57
|
+
arguments?: Record<string, unknown>;
|
|
58
|
+
_meta?: Record<string, unknown>;
|
|
59
|
+
} | void> | {
|
|
60
|
+
name: string;
|
|
61
|
+
arguments?: Record<string, unknown>;
|
|
62
|
+
_meta?: Record<string, unknown>;
|
|
63
|
+
} | void;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Wrap an MCP client so every outbound `callTool` carries an OpenAgent
|
|
67
|
+
* identity envelope.
|
|
68
|
+
*/
|
|
69
|
+
declare function withOpenAgentClient<T extends McpClientLike>(client: T, config: OpenAgentClientConfig): T;
|
|
70
|
+
/**
|
|
71
|
+
* Helper for tests and tools that want to construct the identity
|
|
72
|
+
* envelope manually without going through a full Agent. Returns a `_meta`
|
|
73
|
+
* object that can be merged into a CallToolRequest's params.
|
|
74
|
+
*/
|
|
75
|
+
declare function buildIdentityMeta(identity: {
|
|
76
|
+
did: string;
|
|
77
|
+
proof: string;
|
|
78
|
+
nonce?: string;
|
|
79
|
+
context?: Record<string, unknown>;
|
|
80
|
+
}): Record<string, unknown>;
|
|
81
|
+
|
|
82
|
+
export { type McpClientLike, type OpenAgentClientConfig, buildIdentityMeta, withOpenAgentClient };
|