refactor(mcp): simplify workflow and observation scope
This commit is contained in:
1 parent
2f604a3c3a
commit
34d00fd323
16 files changed
+239
-81
No files matched your search
@@ -7,12 +7,19 @@ import {
|
||||
observeJavaScriptRuntime,
|
||||
} from "../application/JavaScriptRuntimeObservationService.js";
|
||||
import type { PermissionAuthority } from "../application/PermissionAuthority.js";
|
||||
import { resolveEvidenceReferences } from "../application/EvidenceReferenceResolver.js";
|
||||
import { JAVASCRIPT_RUNTIME_OBSERVATION_TOOL_CONTRACTS } from "../contracts/javascriptRuntimeObservationToolContracts.js";
|
||||
import type { ToolContract } from "../contracts/toolContracts.js";
|
||||
import type { AnalysisError } from "../domain/errors.js";
|
||||
import { AnalysisInputError } from "../domain/errors.js";
|
||||
import type { Evidence } from "../domain/evidence.js";
|
||||
import type { Result } from "../domain/result.js";
|
||||
import { observeJavaScriptRuntimeInputSchema } from "../domain/javascriptRuntimeObservation.js";
|
||||
import { err, type Result } from "../domain/result.js";
|
||||
import {
|
||||
javascriptRuntimeTargetListSchema,
|
||||
listJavaScriptRuntimeTargetsInputSchema,
|
||||
observeJavaScriptRuntimeInputSchema,
|
||||
observeJavaScriptRuntimeToolInputSchema,
|
||||
} from "../domain/javascriptRuntimeObservation.js";
|
||||
import type { Logger } from "../logger.js";
|
||||
import { logToolExecution } from "./toolLogging.js";
|
||||
import { toolRegistrationOptions } from "./toolRegistrationOptions.js";
|
||||
@@ -23,6 +30,9 @@ interface RuntimeToolRegistration {
|
||||
readonly runtime: JavaScriptRuntimeObservationPort | undefined;
|
||||
readonly permissionAuthority: PermissionAuthority | undefined;
|
||||
readonly recordEvidence: BinarySessionPort["recordEvidence"] | undefined;
|
||||
readonly evidenceLookup:
|
||||
| ((evidenceId: string) => Evidence | undefined)
|
||||
| undefined;
|
||||
}
|
||||
|
||||
/** Register passive Inspector tools even when policy keeps them unavailable. */
|
||||
@@ -57,17 +67,72 @@ export const registerJavaScriptRuntimeObservationTools = (
|
||||
options,
|
||||
observeContract,
|
||||
{ input, context },
|
||||
(parsed, signal) =>
|
||||
observeJavaScriptRuntime(
|
||||
async (parsed, signal) => {
|
||||
const request = observeJavaScriptRuntimeToolInputSchema.parse(parsed);
|
||||
const source = resolveEvidenceReferences(
|
||||
options.evidenceLookup,
|
||||
[request.discovery_evidence_id],
|
||||
[
|
||||
{
|
||||
operation: "list_javascript_runtime_targets",
|
||||
predicate: "rea.javascript-runtime-target-list/v1",
|
||||
},
|
||||
],
|
||||
);
|
||||
if (!source.ok) return err(source.error);
|
||||
const discovery = source.value[0];
|
||||
if (discovery === undefined) return err(invalidDiscoveryEvidence());
|
||||
const scope = listJavaScriptRuntimeTargetsInputSchema.safeParse(
|
||||
discovery.parameters,
|
||||
);
|
||||
const listed = javascriptRuntimeTargetListSchema.safeParse(
|
||||
discovery.normalized_result,
|
||||
);
|
||||
if (!scope.success || !listed.success)
|
||||
return err(invalidDiscoveryEvidence());
|
||||
if (
|
||||
!listed.data.targets.some(
|
||||
({ target_id }) => target_id === request.target_id,
|
||||
)
|
||||
)
|
||||
return err(
|
||||
new AnalysisInputError("observe_javascript_runtime", undefined, [
|
||||
{
|
||||
path: ["target_id"],
|
||||
reason: "invalid_value",
|
||||
message:
|
||||
"Choose a target returned by the referenced discovery Evidence",
|
||||
},
|
||||
]),
|
||||
);
|
||||
return observeJavaScriptRuntime(
|
||||
options.runtime,
|
||||
options.permissionAuthority,
|
||||
observeJavaScriptRuntimeInputSchema.parse(parsed),
|
||||
observeJavaScriptRuntimeInputSchema.parse({
|
||||
...scope.data,
|
||||
target_id: request.target_id,
|
||||
...(request.runtime_kind === undefined
|
||||
? {}
|
||||
: { runtime_kind: request.runtime_kind }),
|
||||
observation_ms: request.observation_ms,
|
||||
}),
|
||||
{ signal },
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
};
|
||||
|
||||
const invalidDiscoveryEvidence = () =>
|
||||
new AnalysisInputError("observe_javascript_runtime", undefined, [
|
||||
{
|
||||
path: ["discovery_evidence_id"],
|
||||
reason: "invalid_value",
|
||||
message:
|
||||
"The referenced Evidence is not a usable Inspector target discovery",
|
||||
},
|
||||
]);
|
||||
|
||||
const runRuntimeTool = async <Input>(
|
||||
options: RuntimeToolRegistration,
|
||||
contract: ToolContract,
|
||||
|
||||
Reference in new issue
Block a user