@kya-os/mcp-i 1.12.2 → 1.12.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.
@@ -176,69 +176,70 @@ class MCPIRuntime {
176
176
  if (!this.cachedIdentity) {
177
177
  throw new Error("Runtime not initialized - call initialize() first");
178
178
  }
179
- // PHASE 1 INTERCEPTOR: Check delegation BEFORE executing tool
180
- if (this.config.delegation?.enabled && options.requiresDelegation) {
181
- if (!this.delegationVerifier) {
182
- throw new Error("Delegation verifier not configured");
183
- }
184
- if (!options.agentDid) {
185
- throw new Error("agentDid required when requiresDelegation=true");
186
- }
187
- const requiredScopes = options.requiredScopes || [];
188
- // Build auth handshake config
189
- const authConfig = {
190
- delegationVerifier: this.delegationVerifier,
191
- resumeTokenStore: this.resumeTokenStore,
192
- kta: this.config.delegation.authorization?.kta,
193
- bouncer: {
194
- authorizationUrl: this.config.delegation.authorization?.authorizationUrl ||
195
- "https://agentshield.example.com/consent",
196
- resumeTokenTtl: this.config.delegation.authorization?.resumeTokenTtl,
197
- minReputationScore: this.config.delegation.authorization?.minReputationScore,
198
- requireAuthForUnknown: this.config.delegation.authorization?.requireAuthForUnknown,
199
- },
200
- debug: this.config.identity?.environment === "development",
201
- };
202
- // Verify delegation or return authorization hints
203
- const verifyResult = await (0, auth_handshake_1.verifyOrHints)(options.agentDid, requiredScopes, authConfig);
204
- // If not authorized, return needs_authorization error
205
- if (!verifyResult.authorized) {
206
- if (!verifyResult.authError) {
207
- throw new Error('Authorization failed but no authError was provided');
208
- }
209
- return verifyResult.authError;
210
- }
211
- // If authorized, capture delegation metadata for outbound header propagation
212
- if (verifyResult.delegation) {
213
- options.delegationRef = verifyResult.delegation.id;
214
- options.delegationChain = (0, mcp_i_core_1.buildChainString)(verifyResult.delegation);
215
- options.delegationScopes = options.requiredScopes ?? [];
216
- const identity = await this.identityManager.ensureIdentity();
217
- (0, request_context_1.setDelegationContextFromIdentity)({
218
- delegationId: options.delegationRef,
219
- delegationChain: options.delegationChain,
220
- delegationScopes: options.delegationScopes,
221
- // JWS-compact DelegationCredential VC from the verified result —
222
- // emitted on KYA-OS-Delegation-Credential when the verifier
223
- // returned a presentable credential.
224
- delegationCredential: verifyResult.credential?.credential_jwt,
225
- identity,
226
- });
227
- }
228
- }
229
- const auditInvocation = this.config.auditTrail
230
- ? await (0, mcp_i_runtime_1.beginRuntimeToolAudit)(this.config.auditTrail, {
231
- toolName: request.method,
232
- args: request.params,
233
- session: {
234
- ...session,
235
- id: session.sessionId,
236
- },
237
- })
238
- : undefined;
239
- // Capture before async boundary — TS narrowing doesn't survive across async callbacks
179
+ // Isolate verification and delegation setup before either can change a caller's
180
+ // context. The tool and proof generation then share this call's own store.
240
181
  const cachedIdentity = this.cachedIdentity;
241
182
  return (0, request_context_1.runWithContext)({ session: session ?? undefined }, async () => {
183
+ // PHASE 1 INTERCEPTOR: Check delegation BEFORE executing tool
184
+ if (this.config.delegation?.enabled && options.requiresDelegation) {
185
+ if (!this.delegationVerifier) {
186
+ throw new Error("Delegation verifier not configured");
187
+ }
188
+ if (!options.agentDid) {
189
+ throw new Error("agentDid required when requiresDelegation=true");
190
+ }
191
+ const requiredScopes = options.requiredScopes || [];
192
+ // Build auth handshake config
193
+ const authConfig = {
194
+ delegationVerifier: this.delegationVerifier,
195
+ resumeTokenStore: this.resumeTokenStore,
196
+ kta: this.config.delegation.authorization?.kta,
197
+ bouncer: {
198
+ authorizationUrl: this.config.delegation.authorization?.authorizationUrl ||
199
+ "https://agentshield.example.com/consent",
200
+ resumeTokenTtl: this.config.delegation.authorization?.resumeTokenTtl,
201
+ minReputationScore: this.config.delegation.authorization?.minReputationScore,
202
+ requireAuthForUnknown: this.config.delegation.authorization?.requireAuthForUnknown,
203
+ },
204
+ debug: this.config.identity?.environment === "development",
205
+ };
206
+ // Verify delegation or return authorization hints
207
+ const verifyResult = await (0, auth_handshake_1.verifyOrHints)(options.agentDid, requiredScopes, authConfig);
208
+ // If not authorized, return needs_authorization error
209
+ if (!verifyResult.authorized) {
210
+ if (!verifyResult.authError) {
211
+ throw new Error('Authorization failed but no authError was provided');
212
+ }
213
+ return verifyResult.authError;
214
+ }
215
+ // If authorized, capture delegation metadata for outbound header propagation
216
+ if (verifyResult.delegation) {
217
+ options.delegationRef = verifyResult.delegation.id;
218
+ options.delegationChain = (0, mcp_i_core_1.buildChainString)(verifyResult.delegation);
219
+ options.delegationScopes = options.requiredScopes ?? [];
220
+ const identity = await this.identityManager.ensureIdentity();
221
+ (0, request_context_1.setDelegationContextFromIdentity)({
222
+ delegationId: options.delegationRef,
223
+ delegationChain: options.delegationChain,
224
+ delegationScopes: options.delegationScopes,
225
+ // JWS-compact DelegationCredential VC from the verified result —
226
+ // emitted on KYA-OS-Delegation-Credential when the verifier
227
+ // returned a presentable credential.
228
+ delegationCredential: verifyResult.credential?.credential_jwt,
229
+ identity,
230
+ });
231
+ }
232
+ }
233
+ const auditInvocation = this.config.auditTrail
234
+ ? await (0, mcp_i_runtime_1.beginRuntimeToolAudit)(this.config.auditTrail, {
235
+ toolName: request.method,
236
+ args: request.params,
237
+ session: {
238
+ ...session,
239
+ id: session.sessionId,
240
+ },
241
+ })
242
+ : undefined;
242
243
  try {
243
244
  // Execute the tool (only if delegation check passed)
244
245
  let data = await toolHandler(request);