Files
Void-Homelab/lib/mcp/context.js
2026-06-04 20:08:14 +10:00

41 lines
1.3 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Builds the tool ctx object from environment variables.
* Used by companion-stdio.js when a tool call arrives, so each call reads
* fresh env values (useful if the process restarts or env is injected at launch).
*
* Environment variables:
* VOID_AGENT_JSON JSON-serialised agent actor object (required for most tools)
* VOID_SPACE_ID UUID of the active space
* VOID_VIEW_JSON JSON-serialised view object (optional)
*/
export function buildCtxFromEnv(env = process.env) {
return {
agent: env.VOID_AGENT_JSON ? JSON.parse(env.VOID_AGENT_JSON) : null,
space_id: env.VOID_SPACE_ID || null,
view: env.VOID_VIEW_JSON ? JSON.parse(env.VOID_VIEW_JSON) : null,
actor: { kind: 'user', id: null }
};
}
/**
* Builds the tool ctx for an authenticated EXTERNAL agent. The Space is taken
* from the agent's own scope (never client-supplied) and `spaceScoped` is set
* so read() denies entities it can't prove are in-Space.
* @param {{id:string, capabilities?:object, scopes?:object}} agent
*/
export function buildCtxFromAgent(agent) {
const actor = {
kind: 'agent',
id: agent.id,
capabilities: agent.capabilities || {},
scopes: agent.scopes || {}
};
return {
agent: actor,
space_id: (agent.scopes && agent.scopes.space_id) || null,
view: null,
spaceScoped: true,
actor
};
}