@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
82 lines • 3.87 kB
JavaScript
import { getMcpMrtrPresentation } from "../../../mcp-mrtr-presentations.js";
import { McpMrtrError } from "./modern/errors.js";
import { createMcpRequestOrigin } from "./request-origin.js";
/** A context's elicitation UI, read per request: a host that offers none answers no form (`hasUI` false). */
export function elicitationUiOf(ctx) {
return () => (ctx.hasUI === false ? undefined : ctx.ui);
}
/**
* A further scope joins the binding and returns its session id; the binding's own context, or a context with no
* session manager, joins nothing.
*/
export function joinBindingScope(binding, ctx) {
const sessionManager = ctx.sessionManager;
if (sessionManager === undefined || sessionManager === binding.context.sessionManager)
return undefined;
const sessionId = sessionManager.getSessionId?.();
binding.scopes.set(sessionManager, {
sessionId,
presentation: ctx.ui === undefined ? undefined : getMcpMrtrPresentation(ctx.ui),
ui: elicitationUiOf(ctx),
});
return { sessionId };
}
/** A joined scope presents its own forms; the binding's own context keeps the binding's presentation. */
export function bindScopePresentation(binding, presentation, sessionManager) {
const scope = sessionManager === undefined ? undefined : binding.scopes.get(sessionManager);
if (scope === undefined)
binding.presentation = presentation;
else
scope.presentation = presentation;
}
export function bindingServesScope(binding, sessionManager) {
return sessionManager !== undefined && binding.scopes.has(sessionManager);
}
export function bindingServesScopeSession(binding, sessionId) {
return [...binding.scopes.values()].some((scope) => scope.sessionId === sessionId);
}
/**
* The scope a call belongs to while its binding serves several. A binding serving its own context alone resolves to
* none, and its requests carry no scope: the single-session path is unchanged.
*/
function callerScope(binding, caller) {
const key = caller?.sessionManager;
if (binding.scopes.size === 0 || key === undefined)
return undefined;
const joined = binding.scopes.get(key);
if (joined !== undefined)
return { key, presentation: joined.presentation, ui: joined.ui };
return key === binding.context.sessionManager
? { key, presentation: binding.presentation, ui: binding.ui }
: undefined;
}
/** A request's origin for `caller`: its scope's presentation and form UI, under the binding's own lifetime. */
export function scopedRequestOrigin(binding, entry, caller, isCurrent) {
const source = binding.config.servers[entry.name]?.source;
const scope = callerScope(binding, caller);
const presentation = scope === undefined ? binding.presentation : scope.presentation;
return createMcpRequestOrigin({
binding,
connection: entry.connection,
signal: AbortSignal.any([binding.lifetime.signal, entry.connection.requestLifetimeSignal]),
presentation,
privateFormsEnforced: presentation?.privateForms !== undefined,
privateForm: presentation?.privateForms?.capture(),
scope: scope?.key,
elicitationUi: scope?.ui,
serverLabel: entry.name,
serverSource: source === "project" || source === "claude"
? "project"
: source === "skill" || source === "extension"
? "plugin"
: "user",
isCurrent,
resolveAuthority: async (signal) => {
if (await entry.generationChanged?.())
throw new McpMrtrError("MCP_MRTR_CONTEXT_CHANGED");
// Same observed-revocation boundary as ordinary MCP requests. Never prompts for a new grant.
return await entry.connection.projectPolicy?.resolve(signal, false);
},
});
}
//# sourceMappingURL=service-scopes.js.map