a2a-bridge-for-mcp-clients 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +43 -3
- package/dist/addressing.js +46 -11
- package/dist/profiles.js +85 -0
- package/dist/server.js +29 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -191,8 +191,10 @@ Profiles live in `~/.config/a2a-bridge-for-mcp-clients/agents.json`. Use `A2A_BR
|
|
|
191
191
|
| `quirks.streamDeltasWithoutAppend` | For agents that stream text deltas with `append: false` while the artifact is still open. The A2A spec says such chunks replace the text, so without this flag only the last chunk would remain. It's on for Rovo (its preset, or any profile whose endpoint host is `a2a.atlassian.com`) and off for everyone else. When a stream looks like this but the flag is off, the bridge logs a hint and keeps following the spec. |
|
|
192
192
|
| `forbiddenHint` | Advice appended to 403 errors. |
|
|
193
193
|
| `description` | Shown to the model in the tool description. |
|
|
194
|
-
| `targets` | Agents or sub-agents behind this endpoint, each with optional `agent`, `tenant`, `extension`, `instruction`, `metadata`, `aliases`, `keepAlias` and `description`. See [Addressing agents behind an endpoint](#addressing-agents-behind-an-endpoint). |
|
|
194
|
+
| `targets` | Agents or sub-agents behind this endpoint, each with optional `agent`, `tenant`, `extension`, `instruction`, `metadata`, `siteMetadata`, `aliases`, `keepAlias` and `description`. See [Addressing agents behind an endpoint](#addressing-agents-behind-an-endpoint). |
|
|
195
|
+
| `defaultTarget` | A target every message to this agent goes to, whatever alias or `target` the message names. See [Enforced default target](#enforced-default-target). |
|
|
195
196
|
| `metadata` | Raw `message.metadata` added to every message to this agent (non-standard; see [Raw metadata](#raw-metadata)). |
|
|
197
|
+
| `siteMetadata` | Raw metadata for one site only, merged over `metadata`. See [Raw metadata](#raw-metadata). |
|
|
196
198
|
| `sites` | Optional: sites (e.g. `abc.atlassian.net`) to sign in to separately; sites signed in to with `login --site` are added automatically. See [Sites](#sites-which-atlassian-site-answered). |
|
|
197
199
|
|
|
198
200
|
Secrets never go in the profile file. `clientSecretEnv` and `valueEnv` name environment variables, and those names must start with `A2A_BRIDGE_SECRET_`. That way a hostile profile can't send arbitrary environment secrets to its endpoint.
|
|
@@ -268,7 +270,44 @@ A configured route that the card does not advertise is skipped (the log says why
|
|
|
268
270
|
|
|
269
271
|
### Raw metadata
|
|
270
272
|
|
|
271
|
-
`metadata` on a profile or a target is added to `message.metadata` on every addressed or unaddressed message of that agent, whatever route applies
|
|
273
|
+
`metadata` on a profile or a target is added to `message.metadata` on every addressed or unaddressed message of that agent, whatever route applies. This is a non-standard, last-resort channel for routers that expect fields such as `agentId`; prefer an extension where you control the receiving side.
|
|
274
|
+
|
|
275
|
+
**Per site.** `siteMetadata` (on a profile or a target) holds metadata for one site at a time, keyed by site (`abc` is short for `abc.atlassian.net`). The keys must be among the agent's `sites` when it lists any. It applies to the site that answers, as confirmed at sign-in, and suits values that differ per installation, such as the ID an app's agent gets on each site:
|
|
276
|
+
|
|
277
|
+
```json
|
|
278
|
+
"engram": {
|
|
279
|
+
"metadata": { "agentKey": "engram-agent" },
|
|
280
|
+
"siteMetadata": {
|
|
281
|
+
"abc.atlassian.net": { "agentId": "…" },
|
|
282
|
+
"xyz.atlassian.net": { "agentId": "…" }
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
**Merge order**, later values winning: the agent's `metadata`, the agent's `siteMetadata` for the site, the target's `metadata`, the target's `siteMetadata` for the site. A site without an entry gets only the site-independent values. Every merge must fit in 4 KB of JSON; the profile file is rejected otherwise.
|
|
288
|
+
|
|
289
|
+
Metadata is not saved with conversations: follow-ups rebuild it from the current profile for the site that answers them.
|
|
290
|
+
|
|
291
|
+
### Enforced default target
|
|
292
|
+
|
|
293
|
+
`defaultTarget` on a profile names one of its targets. Every message to that agent then goes to that target:
|
|
294
|
+
- **Messages without an alias or `target`** go there too, so its `instruction` and metadata apply to everything sent to the agent.
|
|
295
|
+
- **Another target named** by alias or by the `target` parameter is replaced. The answer's `note:` says so, and the other alias stays part of the message text.
|
|
296
|
+
- **Follow-ups** use it as well. `get_agent_task` and `cancel_agent_task` use it for tasks without saved routing; a task the bridge saved keeps the routing it was created with, since that is where the agent keeps it.
|
|
297
|
+
|
|
298
|
+
It is per agent: an alias of another agent's target still selects that agent. A target of another agent may not route to an agent with a `defaultTarget` (the profile file is rejected), so the enforcement cannot be bypassed that way. For example, to make every Rovo message go through one Rovo agent:
|
|
299
|
+
|
|
300
|
+
```json
|
|
301
|
+
"rovo": {
|
|
302
|
+
"preset": "rovo",
|
|
303
|
+
"defaultTarget": "engram",
|
|
304
|
+
"targets": {
|
|
305
|
+
"engram": { "instruction": "@{target} Use the {target} agent to accomplish the following task: {message}" }
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
An instruction is only a request to the receiving agent's model; whether Rovo hands the work to a named agent is up to Rovo.
|
|
272
311
|
|
|
273
312
|
### Rules
|
|
274
313
|
|
|
@@ -276,7 +315,8 @@ A configured route that the card does not advertise is skipped (the log says why
|
|
|
276
315
|
- **Custom aliases** go in `"aliases": ["/mem", "@memory"]`, which replace the defaults.
|
|
277
316
|
- **Unknown `/…` or `@…` prefixes** are sent unchanged, since they may be the receiving agent's own commands.
|
|
278
317
|
- **Aliases are unique across all profiles**, so an alias also selects its agent when `agent` is omitted.
|
|
279
|
-
- **
|
|
318
|
+
- **Target names are unique per receiving agent**: two targets that reach the same agent (one of its own, one routed to it from another profile) can't share a name, since conversations remember the target by name.
|
|
319
|
+
- **Metadata is sent as-is** (up to 4 KB of JSON per object and per merge), so don't put secrets in it; secrets belong in `A2A_BRIDGE_SECRET_*` variables.
|
|
280
320
|
|
|
281
321
|
## How it works
|
|
282
322
|
|
package/dist/addressing.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { declaredExtensions, isV1, tenantEndpoint } from './card.js';
|
|
2
2
|
import { sameOrigin } from './http.js';
|
|
3
|
+
import { rawMetadata } from './profiles.js';
|
|
3
4
|
/** A leading `/name` or `@name` token, followed by whitespace or the end of the message. */
|
|
4
5
|
const LEADING_ALIAS = /^\s*([/@][A-Za-z0-9][\w-]*)(?=\s|$)\s*/;
|
|
5
6
|
export function leadingAlias(text) {
|
|
@@ -24,11 +25,48 @@ function receiver(profiles, owner, target) {
|
|
|
24
25
|
* - An explicit `targetName` wins; without `agent` it must be unique across profiles.
|
|
25
26
|
* - Otherwise a leading alias selects a target (and, without `agent`, its agent).
|
|
26
27
|
* - Unknown `/…` or `@…` prefixes are left alone: they may be the agent's own commands.
|
|
28
|
+
* - An agent with a `defaultTarget` sends every message to that target, whatever the
|
|
29
|
+
* alias or `targetName` asked for (the agent itself is still chosen as above).
|
|
27
30
|
*/
|
|
28
31
|
export function resolveAddressing(profiles, message, { agent, targetName } = {}) {
|
|
29
32
|
const fallback = profiles.agents.get(agent ?? profiles.defaultAgent);
|
|
30
33
|
if (!fallback)
|
|
31
34
|
throw new Error(`Unknown agent "${agent}"`);
|
|
35
|
+
const chosen = chooseTarget(profiles, message, { agent, targetName });
|
|
36
|
+
const owner = chosen?.owner ?? fallback;
|
|
37
|
+
const enforced = owner.defaultTarget ? owner.targets.find((t) => t.name === owner.defaultTarget) : undefined;
|
|
38
|
+
if (enforced && enforced !== chosen?.target) {
|
|
39
|
+
// Only the enforced target's own alias is stripped; any other alias stays part of the message.
|
|
40
|
+
const lead = leadingAlias(message);
|
|
41
|
+
const text = lead && enforced.aliases.includes(lead.alias) && !enforced.keepAlias ? lead.rest : message;
|
|
42
|
+
return {
|
|
43
|
+
profile: receiver(profiles, owner, enforced),
|
|
44
|
+
target: enforced,
|
|
45
|
+
enforced: true,
|
|
46
|
+
...(chosen ? { asked: true } : {}),
|
|
47
|
+
text: requireText(text, enforced),
|
|
48
|
+
...(chosen ? { note: `target "${chosen.target.name}" was replaced by agent ${owner.name}'s enforced default target "${enforced.name}"` } : {}),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
if (!chosen)
|
|
52
|
+
return { profile: fallback, text: message };
|
|
53
|
+
const enforcedToo = enforced === chosen.target ? { enforced: true } : {};
|
|
54
|
+
return { profile: receiver(profiles, chosen.owner, chosen.target), target: chosen.target, text: chosen.text, asked: true, ...enforcedToo };
|
|
55
|
+
}
|
|
56
|
+
/** The configured target named `name` whose messages are received by `profile` (its own, or one routed to it). */
|
|
57
|
+
export function targetFor(profiles, profile, name) {
|
|
58
|
+
const own = profile.targets.find((t) => t.name === name && !t.agent);
|
|
59
|
+
if (own)
|
|
60
|
+
return own;
|
|
61
|
+
for (const owner of profiles.agents.values()) {
|
|
62
|
+
const routed = owner.targets.find((t) => t.name === name && t.agent === profile.name);
|
|
63
|
+
if (routed)
|
|
64
|
+
return routed;
|
|
65
|
+
}
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
/** The target a message asks for, by `targetName` or a leading alias, with the agent that owns it. */
|
|
69
|
+
function chooseTarget(profiles, message, { agent, targetName }) {
|
|
32
70
|
if (targetName) {
|
|
33
71
|
const found = findTarget(profiles, agent, (t) => t.name === targetName);
|
|
34
72
|
if (found.length === 0)
|
|
@@ -40,21 +78,17 @@ export function resolveAddressing(profiles, message, { agent, targetName } = {})
|
|
|
40
78
|
// A leading alias of the same target is redundant; drop it the same way.
|
|
41
79
|
const lead = leadingAlias(message);
|
|
42
80
|
const text = lead && target.aliases.includes(lead.alias) && !target.keepAlias ? lead.rest : message;
|
|
43
|
-
return {
|
|
81
|
+
return { owner: profile, target, text: requireText(text, target) };
|
|
44
82
|
}
|
|
45
83
|
const lead = leadingAlias(message);
|
|
46
84
|
if (lead) {
|
|
47
85
|
const found = findTarget(profiles, agent, (t) => t.aliases.includes(lead.alias));
|
|
48
86
|
if (found.length === 1) {
|
|
49
87
|
const { profile, target } = found[0];
|
|
50
|
-
return {
|
|
51
|
-
profile: receiver(profiles, profile, target),
|
|
52
|
-
target,
|
|
53
|
-
text: requireText(target.keepAlias ? message : lead.rest, target),
|
|
54
|
-
};
|
|
88
|
+
return { owner: profile, target, text: requireText(target.keepAlias ? message : lead.rest, target) };
|
|
55
89
|
}
|
|
56
90
|
}
|
|
57
|
-
return
|
|
91
|
+
return undefined;
|
|
58
92
|
}
|
|
59
93
|
function requireText(text, target) {
|
|
60
94
|
if (!text.trim())
|
|
@@ -70,13 +104,14 @@ export function renderInstruction(template, target, message) {
|
|
|
70
104
|
* Picks the route for an addressed message from what the receiving agent
|
|
71
105
|
* advertises. `card` is undefined for profiles with an explicit endpoint:
|
|
72
106
|
* then a configured `tenant` (v1.0) is trusted as is, while an extension,
|
|
73
|
-
* which must be declared, is not used.
|
|
107
|
+
* which must be declared, is not used. `site` (the hostname that answers)
|
|
108
|
+
* selects per-site metadata.
|
|
74
109
|
*/
|
|
75
|
-
export function planRoute(addressed, { card, endpoint }) {
|
|
110
|
+
export function planRoute(addressed, { card, endpoint, site }) {
|
|
76
111
|
const { profile, target, text } = addressed;
|
|
77
112
|
const notes = [];
|
|
78
|
-
// Raw metadata: the receiving agent's
|
|
79
|
-
const raw =
|
|
113
|
+
// Raw metadata: the receiving agent's, then the target's (target wins), each with its per-site part.
|
|
114
|
+
const raw = rawMetadata(profile, target, site);
|
|
80
115
|
const finish = (plan, extensionData = {}) => {
|
|
81
116
|
const metadata = { ...extensionData, ...raw };
|
|
82
117
|
return { ...plan, notes, ...(Object.keys(metadata).length ? { metadata } : {}) };
|
package/dist/profiles.js
CHANGED
|
@@ -130,6 +130,11 @@ const RawMetadata = z
|
|
|
130
130
|
return false;
|
|
131
131
|
}
|
|
132
132
|
}, { message: `must be JSON of at most ${MAX_TARGET_METADATA_BYTES} bytes` });
|
|
133
|
+
/** Raw metadata per site (`abc` is short for `abc.atlassian.net`), merged over `metadata` for the site that answers. */
|
|
134
|
+
const SiteMetadata = z
|
|
135
|
+
.record(z.string().max(253).refine((s) => normaliseSite(s) !== undefined, 'must be a hostname such as abc.atlassian.net'), RawMetadata)
|
|
136
|
+
.refine((m) => Object.keys(m).length > 0, { message: 'must not be empty' })
|
|
137
|
+
.refine((m) => Object.keys(m).length <= 20, { message: 'at most 20 sites' });
|
|
133
138
|
/** An absolute URI identifying an A2A extension (as declared in agent cards). */
|
|
134
139
|
const extensionUri = z.string().max(500).regex(/^[a-z][a-z0-9+.-]*:\/\/\S+$/i, 'must be an absolute URI');
|
|
135
140
|
const TargetSchema = z
|
|
@@ -160,6 +165,8 @@ const TargetSchema = z
|
|
|
160
165
|
.optional(),
|
|
161
166
|
/** Added to `message.metadata` as-is, whichever route applies. */
|
|
162
167
|
metadata: RawMetadata.optional(),
|
|
168
|
+
/** Like `metadata`, but only for the named site; merged over it. */
|
|
169
|
+
siteMetadata: SiteMetadata.optional(),
|
|
163
170
|
/** Defaults to `/<name>` and `@<name>`. */
|
|
164
171
|
aliases: z.array(z.string().regex(ALIAS, 'aliases look like /name or @name (lowercase)')).min(1).max(10).optional(),
|
|
165
172
|
description: z.string().max(300).optional(),
|
|
@@ -188,6 +195,13 @@ const ProfileSchema = z
|
|
|
188
195
|
targets: z.record(z.string(), TargetSchema).optional(),
|
|
189
196
|
/** Raw, non-standard `message.metadata` added to every message for this agent. */
|
|
190
197
|
metadata: RawMetadata.optional(),
|
|
198
|
+
/** Like `metadata`, but only for the named site; merged over it. */
|
|
199
|
+
siteMetadata: SiteMetadata.optional(),
|
|
200
|
+
/**
|
|
201
|
+
* A target every message to this agent goes to, whatever the message's alias or the
|
|
202
|
+
* `target` parameter says. Must be one of this agent's targets.
|
|
203
|
+
*/
|
|
204
|
+
defaultTarget: z.string().regex(/^[a-z0-9][a-z0-9_-]{0,63}$/).optional(),
|
|
191
205
|
/**
|
|
192
206
|
* Sites (e.g. Atlassian Cloud sites) to sign in to separately, each with its
|
|
193
207
|
* own credentials. Hostnames; `abc` is short for `abc.atlassian.net`.
|
|
@@ -240,6 +254,8 @@ export function resolveProfile(name, input) {
|
|
|
240
254
|
quirks: merged.quirks ?? {},
|
|
241
255
|
forbiddenHint: merged.forbiddenHint,
|
|
242
256
|
...(merged.metadata ? { metadata: merged.metadata } : {}),
|
|
257
|
+
...(merged.siteMetadata ? { siteMetadata: normaliseSiteKeys(`Agent "${name}"`, merged.siteMetadata) } : {}),
|
|
258
|
+
...(merged.defaultTarget ? { defaultTarget: merged.defaultTarget } : {}),
|
|
243
259
|
sites: uniqueSites(name, merged.sites ?? []),
|
|
244
260
|
targets: Object.entries(merged.targets ?? {}).map(([targetName, t]) => {
|
|
245
261
|
if (!AGENT_NAME.test(targetName)) {
|
|
@@ -253,6 +269,7 @@ export function resolveProfile(name, input) {
|
|
|
253
269
|
...(t.extension ? { extension: t.extension } : {}),
|
|
254
270
|
...(t.instruction ? { instruction: t.instruction } : {}),
|
|
255
271
|
...(t.metadata ? { metadata: t.metadata } : {}),
|
|
272
|
+
...(t.siteMetadata ? { siteMetadata: normaliseSiteKeys(`Target ${name}/${targetName}`, t.siteMetadata) } : {}),
|
|
256
273
|
description: t.description,
|
|
257
274
|
keepAlias: t.keepAlias ?? false,
|
|
258
275
|
};
|
|
@@ -266,6 +283,28 @@ function uniqueSites(agent, sites) {
|
|
|
266
283
|
throw new Error(`Agent "${agent}": site ${duplicate} is listed twice`);
|
|
267
284
|
return hosts;
|
|
268
285
|
}
|
|
286
|
+
function normaliseSiteKeys(owner, bySite) {
|
|
287
|
+
const out = Object.create(null);
|
|
288
|
+
for (const [site, metadata] of Object.entries(bySite)) {
|
|
289
|
+
const host = normaliseSite(site);
|
|
290
|
+
if (Object.hasOwn(out, host))
|
|
291
|
+
throw new Error(`${owner}: siteMetadata lists ${host} twice`);
|
|
292
|
+
out[host] = metadata;
|
|
293
|
+
}
|
|
294
|
+
return out;
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* The raw `message.metadata` for a message to `profile` (the agent that receives it), optionally
|
|
298
|
+
* through `target`, answered by `site`. Later entries win: the agent's metadata, its per-site
|
|
299
|
+
* metadata, the target's metadata, the target's per-site metadata.
|
|
300
|
+
*/
|
|
301
|
+
export function rawMetadata(profile, target, site) {
|
|
302
|
+
const forSite = (bySite) => site && bySite && Object.hasOwn(bySite, site) ? bySite[site] : undefined;
|
|
303
|
+
return { ...profile.metadata, ...forSite(profile.siteMetadata), ...target?.metadata, ...forSite(target?.siteMetadata) };
|
|
304
|
+
}
|
|
305
|
+
function metadataBytes(m) {
|
|
306
|
+
return Buffer.byteLength(JSON.stringify(m));
|
|
307
|
+
}
|
|
269
308
|
function formatIssues(error) {
|
|
270
309
|
return error.issues.map((i) => ` ${i.path.join('.') || '(root)'}: ${i.message}`).join('\n');
|
|
271
310
|
}
|
|
@@ -290,6 +329,52 @@ export function parseProfiles(raw, source = 'profiles') {
|
|
|
290
329
|
if (target.agent === profile.name) {
|
|
291
330
|
throw new Error(`Invalid ${source}: target ${profile.name}/${target.name} routes to its own agent; omit "agent"`);
|
|
292
331
|
}
|
|
332
|
+
// Enforcement must not be bypassed by reaching the agent through another agent's target.
|
|
333
|
+
const routedTo = target.agent ? agents.get(target.agent) : undefined;
|
|
334
|
+
if (routedTo?.defaultTarget) {
|
|
335
|
+
throw new Error(`Invalid ${source}: target ${profile.name}/${target.name} routes to agent ${routedTo.name}, which has an enforced default target; address agent ${routedTo.name} directly`);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
if (profile.defaultTarget && !profile.targets.some((t) => t.name === profile.defaultTarget)) {
|
|
339
|
+
throw new Error(`Invalid ${source}: default target "${profile.defaultTarget}" of agent ${profile.name} is not one of its targets`);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
// Per-site metadata must name a site the receiving agent signs in to (when it lists sites),
|
|
343
|
+
// and every merge must stay within the metadata size limit.
|
|
344
|
+
for (const profile of agents.values()) {
|
|
345
|
+
const checkSites = (owner, receiver, bySite) => {
|
|
346
|
+
for (const site of Object.keys(bySite ?? {})) {
|
|
347
|
+
if (receiver.sites.length && !receiver.sites.includes(site)) {
|
|
348
|
+
throw new Error(`Invalid ${source}: ${owner} has siteMetadata for ${site}, which is not one of agent ${receiver.name}'s sites`);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
checkSites(`agent ${profile.name}`, profile, profile.siteMetadata);
|
|
353
|
+
const routes = [{ owner: `agent ${profile.name}`, receiver: profile }];
|
|
354
|
+
for (const target of profile.targets) {
|
|
355
|
+
const receiver = target.agent ? agents.get(target.agent) : profile;
|
|
356
|
+
checkSites(`target ${profile.name}/${target.name}`, receiver, target.siteMetadata);
|
|
357
|
+
routes.push({ owner: `target ${profile.name}/${target.name}`, receiver, target });
|
|
358
|
+
}
|
|
359
|
+
for (const { owner, receiver, target } of routes) {
|
|
360
|
+
const sites = new Set([...Object.keys(receiver.siteMetadata ?? {}), ...Object.keys(target?.siteMetadata ?? {})]);
|
|
361
|
+
for (const site of [undefined, ...sites]) {
|
|
362
|
+
if (metadataBytes(rawMetadata(receiver, target, site)) > MAX_TARGET_METADATA_BYTES) {
|
|
363
|
+
throw new Error(`Invalid ${source}: ${owner}: merged metadata${site ? ` for ${site}` : ''} exceeds ${MAX_TARGET_METADATA_BYTES} bytes`);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
// Conversations remember a target by name, so a name must identify one target per receiving agent.
|
|
369
|
+
const reaching = new Map();
|
|
370
|
+
for (const profile of agents.values()) {
|
|
371
|
+
for (const target of profile.targets) {
|
|
372
|
+
const key = `${target.agent ?? profile.name}\u0000${target.name}`;
|
|
373
|
+
const existing = reaching.get(key);
|
|
374
|
+
if (existing) {
|
|
375
|
+
throw new Error(`Invalid ${source}: targets ${existing} and ${profile.name}/${target.name} both reach agent ${target.agent ?? profile.name} under the same name; rename one`);
|
|
376
|
+
}
|
|
377
|
+
reaching.set(key, `${profile.name}/${target.name}`);
|
|
293
378
|
}
|
|
294
379
|
}
|
|
295
380
|
// An alias must point to exactly one target, so it can also pick the agent.
|
package/dist/server.js
CHANGED
|
@@ -3,7 +3,7 @@ import { dirname } from 'node:path';
|
|
|
3
3
|
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
4
4
|
import { z } from 'zod';
|
|
5
5
|
import { ask, followTask, isSettled, isTaskNotFound } from './a2a.js';
|
|
6
|
-
import { planRoute, resolveAddressing } from './addressing.js';
|
|
6
|
+
import { planRoute, resolveAddressing, targetFor } from './addressing.js';
|
|
7
7
|
import { addSavedSites, connect, signIns } from './bridge.js';
|
|
8
8
|
import { fetchAgentCard, tenantEndpoint } from './card.js';
|
|
9
9
|
import { formatAskResult, formatError, formatTask, wrapUntrusted } from './format.js';
|
|
@@ -25,7 +25,10 @@ const SKILLS_NOTE = "The agent's card is between the <agent-answer> tags. Its op
|
|
|
25
25
|
/** One line per target: name, agent, aliases and description. */
|
|
26
26
|
export function describeTargets(profiles) {
|
|
27
27
|
return [...profiles.agents.values()]
|
|
28
|
-
.flatMap((p) => p.targets.map((t) =>
|
|
28
|
+
.flatMap((p) => p.targets.map((t) => {
|
|
29
|
+
const enforced = p.defaultTarget === t.name ? `; every message to ${p.name} goes here` : '';
|
|
30
|
+
return `- ${t.name} (agent ${p.name}; ${t.aliases.join(', ')}${enforced})${t.description ? `: ${t.description}` : ''}`;
|
|
31
|
+
}))
|
|
29
32
|
.join('\n');
|
|
30
33
|
}
|
|
31
34
|
export function describeAgents(profiles) {
|
|
@@ -37,6 +40,10 @@ export function describeAgents(profiles) {
|
|
|
37
40
|
.join('\n');
|
|
38
41
|
}
|
|
39
42
|
/** Site lines for the metadata block: the site's URL when known, else its cloud id, plus the organization. */
|
|
43
|
+
function joinNotes(...notes) {
|
|
44
|
+
const present = notes.filter((n) => Boolean(n));
|
|
45
|
+
return present.length ? present.join('; ') : undefined;
|
|
46
|
+
}
|
|
40
47
|
export function siteMeta(site) {
|
|
41
48
|
if (!site)
|
|
42
49
|
return {};
|
|
@@ -343,7 +350,8 @@ export function createServer(config, profiles, { now = Date.now } = {}) {
|
|
|
343
350
|
let chosen;
|
|
344
351
|
const profile = profiles.agents.get(agent ?? profiles.defaultAgent);
|
|
345
352
|
try {
|
|
346
|
-
|
|
353
|
+
// An agent's enforced default target routes task calls too.
|
|
354
|
+
if (targetName || profile?.defaultTarget)
|
|
347
355
|
addressed = resolveAddressing(profiles, 'x', { agent, targetName });
|
|
348
356
|
const receiver = addressed?.profile ?? profile;
|
|
349
357
|
if (receiver)
|
|
@@ -378,7 +386,8 @@ export function createServer(config, profiles, { now = Date.now } = {}) {
|
|
|
378
386
|
throw err;
|
|
379
387
|
}
|
|
380
388
|
await sessions.setTaskState(keyOf(profile, bridge), task.id, task.state);
|
|
381
|
-
|
|
389
|
+
const note = addressed?.note && !known ? addressed.note : undefined;
|
|
390
|
+
return formatTask(profile.name, task, timedOut, { ...(target ? { target } : {}), ...siteMeta(identity), ...(note ? { note } : {}) });
|
|
382
391
|
}, chosen);
|
|
383
392
|
};
|
|
384
393
|
const server = new McpServer({ name: APP_NAME, title: DISPLAY_NAME, version: VERSION, description: DESCRIPTION, websiteUrl: REPOSITORY_URL, icons: ICONS }, {
|
|
@@ -420,7 +429,7 @@ export function createServer(config, profiles, { now = Date.now } = {}) {
|
|
|
420
429
|
throw new Error('input needs skill: name the skill the input is for');
|
|
421
430
|
addressed = resolveAddressing(profiles, message, { agent, targetName: target });
|
|
422
431
|
// A site only one agent lists also picks that agent.
|
|
423
|
-
if (site && !agent && !addressed.
|
|
432
|
+
if (site && !agent && !addressed.asked && !addressed.profile.sites.length) {
|
|
424
433
|
const owners = [...profiles.agents.values()].filter((p) => matchSite(site, p.sites));
|
|
425
434
|
if (owners.length === 1)
|
|
426
435
|
addressed = resolveAddressing(profiles, message, { agent: owners[0].name });
|
|
@@ -445,11 +454,20 @@ export function createServer(config, profiles, { now = Date.now } = {}) {
|
|
|
445
454
|
known = undefined;
|
|
446
455
|
// A follow-up without a target continues the conversation's earlier routing.
|
|
447
456
|
const previous = addressed.target ? undefined : known;
|
|
448
|
-
|
|
457
|
+
// Per-site metadata follows the site that answers (as confirmed at sign-in), else the chosen one.
|
|
458
|
+
const answeringHost = identity?.host ?? bridge.site;
|
|
459
|
+
const answeringSite = answeringHost ? normaliseSite(answeringHost) : undefined;
|
|
460
|
+
const plan = planRoute(addressed, { card, endpoint: bridge.endpoint, ...(answeringSite ? { site: answeringSite } : {}) });
|
|
449
461
|
for (const note of plan.notes)
|
|
450
462
|
log(`${profile.name}/${addressed.target?.name}: ${note}; trying the next route`);
|
|
463
|
+
// Metadata is never replayed from the saved conversation: it is rebuilt from the current
|
|
464
|
+
// profile for the site that answers now, so one site's values cannot reach another.
|
|
465
|
+
const previousTarget = previous?.target ? targetFor(profiles, profile, previous.target) : undefined;
|
|
466
|
+
const previousMetadata = previousTarget
|
|
467
|
+
? planRoute({ profile, target: previousTarget, text: '' }, { card, endpoint: bridge.endpoint, ...(answeringSite ? { site: answeringSite } : {}) }).metadata
|
|
468
|
+
: undefined;
|
|
451
469
|
const entry = previous
|
|
452
|
-
? { route: previous.route, routing: await routingFor(bridge, previous.routing), ...(previous.target ? { target: previous.target } : {}), metadata: { ...
|
|
470
|
+
? { route: previous.route, routing: await routingFor(bridge, previous.routing), ...(previous.target ? { target: previous.target } : {}), metadata: { ...previousMetadata, ...plan.metadata } }
|
|
453
471
|
: { route: plan.route, routing: plan.routing, ...(addressed.target ? { target: addressed.target.name } : {}), ...(plan.metadata ? { metadata: plan.metadata } : {}) };
|
|
454
472
|
if (chosen)
|
|
455
473
|
entry.site = chosen;
|
|
@@ -457,7 +475,7 @@ export function createServer(config, profiles, { now = Date.now } = {}) {
|
|
|
457
475
|
// an answer to an interrupted task carries its taskId, and an ended task is never resumed.
|
|
458
476
|
let sendTaskId = taskId;
|
|
459
477
|
let sendContextId = contextId ?? known?.contextId;
|
|
460
|
-
let note;
|
|
478
|
+
let note = addressed.note;
|
|
461
479
|
const open = known && currentTask(known);
|
|
462
480
|
if (!taskId && contextId && open && isInterruptedState(open.state)) {
|
|
463
481
|
// The saved state may be stale (answered elsewhere, expired): ask the agent first.
|
|
@@ -466,7 +484,7 @@ export function createServer(config, profiles, { now = Date.now } = {}) {
|
|
|
466
484
|
await sessions.setTaskState(keyOf(profile, bridge), open.id, live.state);
|
|
467
485
|
if (isInterruptedState(live.state)) {
|
|
468
486
|
sendTaskId = open.id;
|
|
469
|
-
note = `answering task ${open.id}, which is waiting for input (${live.state})
|
|
487
|
+
note = joinNotes(note, `answering task ${open.id}, which is waiting for input (${live.state})`);
|
|
470
488
|
}
|
|
471
489
|
}
|
|
472
490
|
catch (err) {
|
|
@@ -480,7 +498,7 @@ export function createServer(config, profiles, { now = Date.now } = {}) {
|
|
|
480
498
|
if (ended) {
|
|
481
499
|
sendTaskId = undefined;
|
|
482
500
|
sendContextId = known.contextId;
|
|
483
|
-
note = `task ${ended.id} had ended (${ended.state}), so this starts a new task in the same conversation
|
|
501
|
+
note = joinNotes(note, `task ${ended.id} had ended (${ended.state}), so this starts a new task in the same conversation`);
|
|
484
502
|
}
|
|
485
503
|
if (!contextId && !taskId)
|
|
486
504
|
sendContextId = undefined;
|
|
@@ -526,7 +544,6 @@ export function createServer(config, profiles, { now = Date.now } = {}) {
|
|
|
526
544
|
...(entry.target ? { target: entry.target } : {}),
|
|
527
545
|
route: entry.route,
|
|
528
546
|
routing: entry.routing,
|
|
529
|
-
...(entry.metadata && Object.keys(entry.metadata).length ? { metadata: entry.metadata } : {}),
|
|
530
547
|
});
|
|
531
548
|
}
|
|
532
549
|
return formatAskResult(profile.name, result, {
|
|
@@ -645,7 +662,7 @@ export function createServer(config, profiles, { now = Date.now } = {}) {
|
|
|
645
662
|
annotations: { readOnlyHint: true, openWorldHint: true },
|
|
646
663
|
}, ({ agent, target }) => {
|
|
647
664
|
let profileName = agent;
|
|
648
|
-
if (target) {
|
|
665
|
+
if (target || profiles.agents.get(agent ?? profiles.defaultAgent)?.defaultTarget) {
|
|
649
666
|
try {
|
|
650
667
|
profileName = resolveAddressing(profiles, 'x', { agent, targetName: target }).profile.name;
|
|
651
668
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "a2a-bridge-for-mcp-clients",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "Local stdio MCP server that lets MCP clients talk to A2A agents, with an Atlassian Rovo preset",
|
|
5
5
|
"mcpName": "io.github.kelemen-marton/a2a-bridge-for-mcp-clients",
|
|
6
6
|
"keywords": [
|