@requestly/mcp 1.0.3 → 1.0.5
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/.coderabbit.yaml +9 -0
- package/README.md +27 -0
- package/build/index.js +119 -60
- package/package.json +8 -4
- package/src/__tests__/codeApproval.test.ts +863 -0
- package/src/__tests__/deleteApproval.test.ts +270 -0
- package/src/__tests__/openReview.test.ts +71 -0
- package/src/__tests__/ruleSchemas.test.ts +2 -2
- package/src/__tests__/security.test.ts +152 -0
- package/src/__tests__/serverInstructions.test.ts +27 -0
- package/src/__tests__/setup/isolateHome.ts +33 -0
- package/src/__tests__/testIsolation.test.ts +14 -0
- package/src/__tests__/tools.test.ts +22 -51
- package/src/__tests__/untrustedOutput.test.ts +328 -0
- package/src/apiClient.ts +113 -0
- package/src/codeApproval.ts +409 -0
- package/src/humanApproval.ts +70 -0
- package/src/index.ts +2 -16
- package/src/server.ts +23 -0
- package/src/tools/createGroup.ts +10 -10
- package/src/tools/createRule.ts +50 -18
- package/src/tools/deleteGroup.ts +38 -15
- package/src/tools/deleteRule.ts +39 -14
- package/src/tools/getGroups.ts +11 -11
- package/src/tools/getRules.ts +12 -22
- package/src/tools/updateGroup.ts +11 -11
- package/src/tools/updateRule.ts +135 -28
- package/src/types/createRuleSchemas.ts +10 -4
- package/src/types/updateRuleSchemas.ts +7 -8
- package/src/untrustedData.ts +75 -0
- package/vitest.config.mjs +8 -0
package/src/tools/deleteGroup.ts
CHANGED
|
@@ -1,17 +1,31 @@
|
|
|
1
1
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
-
import {
|
|
2
|
+
import { buildResourceUrl, apiErrorResult, resourceIdSchema, readApiJson } from "../apiClient.js";
|
|
3
|
+
import { untrustedDataResult } from "../untrustedData.js";
|
|
4
|
+
import { askHumanToApprove, notApprovedText } from "../humanApproval.js";
|
|
3
5
|
|
|
4
6
|
export function registerDeleteGroupTool(server: McpServer) {
|
|
5
7
|
server.registerTool(
|
|
6
8
|
"delete_group",
|
|
7
9
|
{
|
|
8
10
|
title: "Delete Group",
|
|
9
|
-
description:
|
|
11
|
+
description:
|
|
12
|
+
"Delete a specific group in Requestly using its id. IRREVERSIBLE. The rules inside the " +
|
|
13
|
+
"group are not deleted; they stay in the workspace without a group. " +
|
|
14
|
+
"Before anything is deleted, the user is asked to approve it directly in the MCP host " +
|
|
15
|
+
"(an approval prompt the model cannot answer). If the user does not approve, or the host " +
|
|
16
|
+
"cannot show the prompt, nothing is deleted.",
|
|
10
17
|
inputSchema: {
|
|
11
|
-
id:
|
|
18
|
+
id: resourceIdSchema.describe("Unique identifier of the group to delete."),
|
|
19
|
+
},
|
|
20
|
+
annotations: {
|
|
21
|
+
title: "Delete Group",
|
|
22
|
+
readOnlyHint: false,
|
|
23
|
+
destructiveHint: true,
|
|
24
|
+
idempotentHint: true,
|
|
25
|
+
openWorldHint: true,
|
|
12
26
|
},
|
|
13
27
|
},
|
|
14
|
-
async (args) => {
|
|
28
|
+
async (args, extra) => {
|
|
15
29
|
const apiKey = process.env.REQUESTLY_API_KEY;
|
|
16
30
|
if (!apiKey) {
|
|
17
31
|
return {
|
|
@@ -23,9 +37,20 @@ export function registerDeleteGroupTool(server: McpServer) {
|
|
|
23
37
|
],
|
|
24
38
|
};
|
|
25
39
|
}
|
|
40
|
+
// RQ-3008: the human approves each delete through the host (MCP elicitation).
|
|
41
|
+
// A model-supplied argument can never authorise it.
|
|
42
|
+
const what = `group "${args.id}"`;
|
|
43
|
+
const approval = await askHumanToApprove(
|
|
44
|
+
server,
|
|
45
|
+
`Permanently delete Requestly group "${args.id}"? This cannot be undone. ` +
|
|
46
|
+
`The rules inside it are kept, without a group.`,
|
|
47
|
+
{ relatedRequestId: extra?.requestId, signal: extra?.signal }
|
|
48
|
+
);
|
|
49
|
+
if (!approval.approved) {
|
|
50
|
+
return { content: [{ type: "text", text: notApprovedText(what, approval.reason) }] };
|
|
51
|
+
}
|
|
26
52
|
try {
|
|
27
|
-
const
|
|
28
|
-
const response = await fetch(`https://api2.requestly.io/v1/groups/${id}`,
|
|
53
|
+
const response = await fetch(buildResourceUrl("groups", args.id),
|
|
29
54
|
{
|
|
30
55
|
method: "DELETE",
|
|
31
56
|
headers: {
|
|
@@ -34,15 +59,13 @@ export function registerDeleteGroupTool(server: McpServer) {
|
|
|
34
59
|
},
|
|
35
60
|
}
|
|
36
61
|
);
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
],
|
|
45
|
-
};
|
|
62
|
+
if (!response.ok) {
|
|
63
|
+
// RQ-3025: status only — never reflect the upstream body.
|
|
64
|
+
return await apiErrorResult("delete group", response);
|
|
65
|
+
}
|
|
66
|
+
const parsed = await readApiJson("delete group", response);
|
|
67
|
+
if (!parsed.ok) return parsed.result;
|
|
68
|
+
return untrustedDataResult("delete_group", parsed.data);
|
|
46
69
|
} catch (error) {
|
|
47
70
|
return {
|
|
48
71
|
content: [
|
package/src/tools/deleteRule.ts
CHANGED
|
@@ -1,17 +1,30 @@
|
|
|
1
1
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
-
import {
|
|
2
|
+
import { buildResourceUrl, apiErrorResult, resourceIdSchema, readApiJson } from "../apiClient.js";
|
|
3
|
+
import { untrustedDataResult } from "../untrustedData.js";
|
|
4
|
+
import { askHumanToApprove, notApprovedText } from "../humanApproval.js";
|
|
3
5
|
|
|
4
6
|
export function registerDeleteRuleTool(server: McpServer) {
|
|
5
7
|
server.registerTool(
|
|
6
8
|
"delete_rule",
|
|
7
9
|
{
|
|
8
10
|
title: "Delete Rule",
|
|
9
|
-
description:
|
|
11
|
+
description:
|
|
12
|
+
"Delete a specific rule in Requestly using its ruleId. IRREVERSIBLE. " +
|
|
13
|
+
"Before anything is deleted, the user is asked to approve it directly in the MCP host " +
|
|
14
|
+
"(an approval prompt the model cannot answer). If the user does not approve, or the host " +
|
|
15
|
+
"cannot show the prompt, nothing is deleted.",
|
|
10
16
|
inputSchema: {
|
|
11
|
-
ruleId:
|
|
17
|
+
ruleId: resourceIdSchema.describe("Unique identifier for the rule to delete."),
|
|
18
|
+
},
|
|
19
|
+
annotations: {
|
|
20
|
+
title: "Delete Rule",
|
|
21
|
+
readOnlyHint: false,
|
|
22
|
+
destructiveHint: true,
|
|
23
|
+
idempotentHint: true,
|
|
24
|
+
openWorldHint: true,
|
|
12
25
|
},
|
|
13
26
|
},
|
|
14
|
-
async (args) => {
|
|
27
|
+
async (args, extra) => {
|
|
15
28
|
const apiKey = process.env.REQUESTLY_API_KEY;
|
|
16
29
|
if (!apiKey) {
|
|
17
30
|
return {
|
|
@@ -23,8 +36,20 @@ export function registerDeleteRuleTool(server: McpServer) {
|
|
|
23
36
|
],
|
|
24
37
|
};
|
|
25
38
|
}
|
|
39
|
+
// RQ-3008: the human approves each delete through the host (MCP elicitation).
|
|
40
|
+
// A model-supplied argument can never authorise it.
|
|
41
|
+
const what = `rule "${args.ruleId}"`;
|
|
42
|
+
const approval = await askHumanToApprove(
|
|
43
|
+
server,
|
|
44
|
+
`Permanently delete Requestly rule "${args.ruleId}"? This cannot be undone. ` +
|
|
45
|
+
`Check the rule in the Requestly app first if you're not sure what it is.`,
|
|
46
|
+
{ relatedRequestId: extra?.requestId, signal: extra?.signal }
|
|
47
|
+
);
|
|
48
|
+
if (!approval.approved) {
|
|
49
|
+
return { content: [{ type: "text", text: notApprovedText(what, approval.reason) }] };
|
|
50
|
+
}
|
|
26
51
|
try {
|
|
27
|
-
const response = await fetch(
|
|
52
|
+
const response = await fetch(buildResourceUrl("rules", args.ruleId),
|
|
28
53
|
{
|
|
29
54
|
method: "DELETE",
|
|
30
55
|
headers: {
|
|
@@ -33,15 +58,15 @@ export function registerDeleteRuleTool(server: McpServer) {
|
|
|
33
58
|
},
|
|
34
59
|
}
|
|
35
60
|
);
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
61
|
+
if (!response.ok) {
|
|
62
|
+
// RQ-3025: the success path stringified the upstream body verbatim,
|
|
63
|
+
// which reflected API error payloads into LLM context just as the
|
|
64
|
+
// explicit error branches did.
|
|
65
|
+
return await apiErrorResult("delete rule", response);
|
|
66
|
+
}
|
|
67
|
+
const parsed = await readApiJson("delete rule", response);
|
|
68
|
+
if (!parsed.ok) return parsed.result;
|
|
69
|
+
return untrustedDataResult("delete_rule", parsed.data);
|
|
45
70
|
} catch (error) {
|
|
46
71
|
return {
|
|
47
72
|
content: [
|
package/src/tools/getGroups.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
2
|
import { z } from "zod";
|
|
3
|
+
import { REQUESTLY_API_BASE, apiErrorResult, readApiJson } from "../apiClient.js";
|
|
4
|
+
import { UNTRUSTED_OUTPUT_DESCRIPTION, untrustedDataResult } from "../untrustedData.js";
|
|
3
5
|
|
|
4
6
|
export function registerGetGroupsTool(server: McpServer) {
|
|
5
7
|
server.registerTool(
|
|
6
8
|
"get_groups",
|
|
7
9
|
{
|
|
8
10
|
title: "Get Groups",
|
|
9
|
-
description: "Get all groups in Requestly.",
|
|
11
|
+
description: "Get all groups in Requestly." + UNTRUSTED_OUTPUT_DESCRIPTION,
|
|
10
12
|
inputSchema: {
|
|
11
13
|
offset: z.number().optional().default(0),
|
|
12
14
|
pageSize: z.number().optional().default(30),
|
|
@@ -28,7 +30,7 @@ export function registerGetGroupsTool(server: McpServer) {
|
|
|
28
30
|
const params = new URLSearchParams();
|
|
29
31
|
if (typeof args.offset === "number") params.append("offset", String(args.offset));
|
|
30
32
|
if (typeof args.pageSize === "number") params.append("pageSize", String(args.pageSize));
|
|
31
|
-
const response = await fetch(
|
|
33
|
+
const response = await fetch(`${REQUESTLY_API_BASE}/groups?${params.toString()}`,
|
|
32
34
|
{
|
|
33
35
|
method: "GET",
|
|
34
36
|
headers: {
|
|
@@ -37,15 +39,13 @@ export function registerGetGroupsTool(server: McpServer) {
|
|
|
37
39
|
},
|
|
38
40
|
}
|
|
39
41
|
);
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
],
|
|
48
|
-
};
|
|
42
|
+
if (!response.ok) {
|
|
43
|
+
// RQ-3025: status only — never reflect the upstream body.
|
|
44
|
+
return await apiErrorResult("get groups", response);
|
|
45
|
+
}
|
|
46
|
+
const parsed = await readApiJson("get groups", response);
|
|
47
|
+
if (!parsed.ok) return parsed.result;
|
|
48
|
+
return untrustedDataResult("get_groups", parsed.data);
|
|
49
49
|
} catch (error) {
|
|
50
50
|
return {
|
|
51
51
|
content: [
|
package/src/tools/getRules.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
2
|
import { z } from "zod";
|
|
3
|
+
import { REQUESTLY_API_BASE, buildResourceUrl, apiErrorResult, resourceIdSchema, readApiJson } from "../apiClient.js";
|
|
4
|
+
import { UNTRUSTED_OUTPUT_DESCRIPTION, untrustedDataResult } from "../untrustedData.js";
|
|
3
5
|
|
|
4
6
|
export const getRulesInputSchema = {
|
|
5
|
-
ruleId:
|
|
7
|
+
ruleId: resourceIdSchema.optional().describe("Unique ID of the rule to retrieve. If omitted, retrieves all rules."),
|
|
6
8
|
offset: z.number().int().min(0).optional().describe("Index to start results from (for pagination)."),
|
|
7
9
|
pageSize: z.number().int().min(1).max(75).optional().describe("Number of results to return (max 75)."),
|
|
8
10
|
};
|
|
@@ -13,7 +15,8 @@ export function registerGetRulesTool(server: McpServer) {
|
|
|
13
15
|
{
|
|
14
16
|
title: "Get Rules",
|
|
15
17
|
description:
|
|
16
|
-
"Retrieve all rules or a specific rule from Requestly using its API. Supports pagination and lookup by ruleId."
|
|
18
|
+
"Retrieve all rules or a specific rule from Requestly using its API. Supports pagination and lookup by ruleId." +
|
|
19
|
+
UNTRUSTED_OUTPUT_DESCRIPTION,
|
|
17
20
|
inputSchema: getRulesInputSchema,
|
|
18
21
|
},
|
|
19
22
|
async (args) => {
|
|
@@ -29,9 +32,9 @@ export function registerGetRulesTool(server: McpServer) {
|
|
|
29
32
|
};
|
|
30
33
|
}
|
|
31
34
|
const { ruleId, offset, pageSize } = args;
|
|
32
|
-
let url =
|
|
35
|
+
let url = `${REQUESTLY_API_BASE}/rules`;
|
|
33
36
|
if (ruleId) {
|
|
34
|
-
url
|
|
37
|
+
url = buildResourceUrl("rules", ruleId);
|
|
35
38
|
} else {
|
|
36
39
|
const params = [];
|
|
37
40
|
if (offset !== undefined) params.push(`offset=${offset}`);
|
|
@@ -49,25 +52,12 @@ export function registerGetRulesTool(server: McpServer) {
|
|
|
49
52
|
},
|
|
50
53
|
});
|
|
51
54
|
if (!response.ok) {
|
|
52
|
-
|
|
53
|
-
return
|
|
54
|
-
content: [
|
|
55
|
-
{
|
|
56
|
-
type: "text",
|
|
57
|
-
text: `Failed to get rules: ${response.status} ${errorText}`,
|
|
58
|
-
},
|
|
59
|
-
],
|
|
60
|
-
};
|
|
55
|
+
// RQ-3025: status only — never reflect the upstream body into LLM context.
|
|
56
|
+
return await apiErrorResult("get rules", response);
|
|
61
57
|
}
|
|
62
|
-
const
|
|
63
|
-
return
|
|
64
|
-
|
|
65
|
-
{
|
|
66
|
-
type: "text",
|
|
67
|
-
text: JSON.stringify(data, null, 2),
|
|
68
|
-
},
|
|
69
|
-
],
|
|
70
|
-
};
|
|
58
|
+
const parsed = await readApiJson("get rules", response);
|
|
59
|
+
if (!parsed.ok) return parsed.result;
|
|
60
|
+
return untrustedDataResult("get_rules", parsed.data);
|
|
71
61
|
} catch (error) {
|
|
72
62
|
return {
|
|
73
63
|
content: [
|
package/src/tools/updateGroup.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
2
|
import { z } from "zod";
|
|
3
|
+
import { buildResourceUrl, apiErrorResult, resourceIdSchema, readApiJson } from "../apiClient.js";
|
|
4
|
+
import { untrustedDataResult } from "../untrustedData.js";
|
|
3
5
|
|
|
4
6
|
export function registerUpdateGroupTool(server: McpServer) {
|
|
5
7
|
server.registerTool(
|
|
@@ -8,7 +10,7 @@ export function registerUpdateGroupTool(server: McpServer) {
|
|
|
8
10
|
title: "Update Group",
|
|
9
11
|
description: "Update a specific group in Requestly.",
|
|
10
12
|
inputSchema: {
|
|
11
|
-
id:
|
|
13
|
+
id: resourceIdSchema.describe("Unique identifier of the group to update."),
|
|
12
14
|
name: z.string().describe("New name of the group."),
|
|
13
15
|
status: z.enum(["Active", "Inactive"]).optional().default("Active"),
|
|
14
16
|
isFavourite: z.boolean().optional().default(false),
|
|
@@ -28,7 +30,7 @@ export function registerUpdateGroupTool(server: McpServer) {
|
|
|
28
30
|
}
|
|
29
31
|
try {
|
|
30
32
|
const { id, ...rest } = args;
|
|
31
|
-
const response = await fetch(
|
|
33
|
+
const response = await fetch(buildResourceUrl("groups", id),
|
|
32
34
|
{
|
|
33
35
|
method: "PUT",
|
|
34
36
|
headers: {
|
|
@@ -39,15 +41,13 @@ export function registerUpdateGroupTool(server: McpServer) {
|
|
|
39
41
|
body: JSON.stringify(rest),
|
|
40
42
|
}
|
|
41
43
|
);
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
],
|
|
50
|
-
};
|
|
44
|
+
if (!response.ok) {
|
|
45
|
+
// RQ-3025: status only — never reflect the upstream body.
|
|
46
|
+
return await apiErrorResult("update group", response);
|
|
47
|
+
}
|
|
48
|
+
const parsed = await readApiJson("update group", response);
|
|
49
|
+
if (!parsed.ok) return parsed.result;
|
|
50
|
+
return untrustedDataResult("update_group", parsed.data);
|
|
51
51
|
} catch (error) {
|
|
52
52
|
return {
|
|
53
53
|
content: [
|
package/src/tools/updateRule.ts
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
2
|
import { ruleSchema, schema, ValidatedRuleArgs } from "../types/updateRuleSchemas.js";
|
|
3
|
-
import {
|
|
3
|
+
import { buildResourceUrl, apiErrorResult, readApiJson } from "../apiClient.js";
|
|
4
|
+
import { untrustedDataResult } from "../untrustedData.js";
|
|
5
|
+
import { EDITED_NOTE, applyAccepted, askHumanToAcceptCode, codeItems, codeNotApprovedText, newCode } from "../codeApproval.js";
|
|
6
|
+
|
|
7
|
+
/** Rule types the update schema accepts. */
|
|
8
|
+
const KNOWN_RULE_TYPES: readonly string[] = schema.ruleType.options;
|
|
9
|
+
|
|
10
|
+
/** The parts of a saved rule that matter for an update; any difference means it changed. */
|
|
11
|
+
function ruleVersion(rule: any): string {
|
|
12
|
+
if (!rule || typeof rule !== "object") return "";
|
|
13
|
+
const { modificationDate, name, description, status, ruleType, pairs, groupId } = rule;
|
|
14
|
+
return JSON.stringify([modificationDate, name, description, status, ruleType, pairs, groupId]);
|
|
15
|
+
}
|
|
4
16
|
|
|
5
17
|
export function registerUpdateRuleTool(server: McpServer) {
|
|
6
18
|
server.registerTool(
|
|
@@ -8,11 +20,25 @@ export function registerUpdateRuleTool(server: McpServer) {
|
|
|
8
20
|
{
|
|
9
21
|
title: "Update Rule",
|
|
10
22
|
description:
|
|
11
|
-
"Update an existing rule in Requestly. Requires ruleId and
|
|
12
|
-
|
|
13
|
-
|
|
23
|
+
"Update an existing rule in Requestly. Requires ruleId and ruleType; any field left out " +
|
|
24
|
+
"(name, description, status, pairs) keeps its saved value. If the update adds or changes code " +
|
|
25
|
+
"in a Script, Request or Response rule (inline scripts, script URLs, request/response bodies), " +
|
|
26
|
+
"the user is shown the old and new code (in the MCP host prompt, or in a local file it names, which the user may edit before accepting) and must accept it (an approval prompt " +
|
|
27
|
+
"the model cannot answer); otherwise nothing is changed. Turning a rule on or off, renaming it, " +
|
|
28
|
+
"or changing its URL match does not ask.",
|
|
29
|
+
inputSchema: schema,
|
|
30
|
+
annotations: {
|
|
31
|
+
title: "Update Rule",
|
|
32
|
+
readOnlyHint: false,
|
|
33
|
+
destructiveHint: false,
|
|
34
|
+
idempotentHint: true,
|
|
35
|
+
openWorldHint: true,
|
|
36
|
+
},
|
|
14
37
|
},
|
|
15
|
-
async (
|
|
38
|
+
async (
|
|
39
|
+
args: Record<string, unknown>,
|
|
40
|
+
extra?: { requestId?: string | number; signal?: AbortSignal }
|
|
41
|
+
): Promise<{ content: Array<{ type: "text"; text: string }> }> => {
|
|
16
42
|
const apiKey = process.env.REQUESTLY_API_KEY;
|
|
17
43
|
if (!apiKey) {
|
|
18
44
|
return {
|
|
@@ -37,40 +63,121 @@ export function registerUpdateRuleTool(server: McpServer) {
|
|
|
37
63
|
],
|
|
38
64
|
};
|
|
39
65
|
}
|
|
40
|
-
const { ruleId
|
|
41
|
-
const
|
|
66
|
+
const { ruleId } = validatedArgs;
|
|
67
|
+
const headers = {
|
|
68
|
+
"accept": "application/json",
|
|
69
|
+
"content-type": "application/json",
|
|
70
|
+
"x-api-key": apiKey,
|
|
71
|
+
};
|
|
42
72
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
);
|
|
54
|
-
if (!
|
|
55
|
-
|
|
73
|
+
// Read the rule as saved. api2's PUT replaces the whole rule, so anything
|
|
74
|
+
// the model leaves out is filled from here (before, a missing name became
|
|
75
|
+
// "API: <type> Rule <time>" and a missing status became Active). It is
|
|
76
|
+
// also the "before" side of the code comparison: the model's own account
|
|
77
|
+
// of the saved rule is never trusted.
|
|
78
|
+
const current = await fetch(buildResourceUrl("rules", ruleId), { method: "GET", headers });
|
|
79
|
+
if (!current.ok) {
|
|
80
|
+
// RQ-3025: status only — never reflect the upstream body into LLM context.
|
|
81
|
+
return await apiErrorResult("read the rule before updating it", current);
|
|
82
|
+
}
|
|
83
|
+
const currentRead = await readApiJson("read the rule before updating it", current);
|
|
84
|
+
if (!currentRead.ok) return currentRead.result;
|
|
85
|
+
const currentJson: any = currentRead.data;
|
|
86
|
+
const saved: any = currentJson?.data ?? currentJson;
|
|
87
|
+
if (!saved || typeof saved !== "object" || typeof saved.ruleType !== "string") {
|
|
88
|
+
return { content: [{ type: "text", text: "Failed to update rule: the saved rule could not be read." }] };
|
|
89
|
+
}
|
|
90
|
+
if (saved.ruleType !== validatedArgs.ruleType) {
|
|
56
91
|
return {
|
|
57
92
|
content: [
|
|
58
93
|
{
|
|
59
94
|
type: "text",
|
|
60
|
-
text:
|
|
95
|
+
text:
|
|
96
|
+
// RQ-3019: the saved ruleType is stored data; name it only when it is a known type.
|
|
97
|
+
`Not updated: rule ${ruleId} is ${
|
|
98
|
+
KNOWN_RULE_TYPES.includes(saved.ruleType) ? `a ${saved.ruleType} rule` : "a rule of another type"
|
|
99
|
+
} and a rule's type can't be changed. ` +
|
|
100
|
+
`Create a new ${validatedArgs.ruleType} rule instead.`,
|
|
61
101
|
},
|
|
62
102
|
],
|
|
63
103
|
};
|
|
64
104
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
105
|
+
|
|
106
|
+
const body: Record<string, unknown> = {
|
|
107
|
+
name: validatedArgs.name ?? saved.name,
|
|
108
|
+
description: validatedArgs.description ?? saved.description,
|
|
109
|
+
objectType: "rule",
|
|
110
|
+
ruleType: saved.ruleType,
|
|
111
|
+
status: validatedArgs.status ?? saved.status,
|
|
112
|
+
pairs: validatedArgs.pairs ?? saved.pairs,
|
|
113
|
+
};
|
|
114
|
+
if (saved.groupId) {
|
|
115
|
+
body.groupId = saved.groupId;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// RQ-3011 / RQ-3021: new or changed code needs the user's Accept in the host.
|
|
119
|
+
const before = codeItems(saved.ruleType, saved.pairs);
|
|
120
|
+
const after = codeItems(saved.ruleType, body.pairs);
|
|
121
|
+
const added = newCode(before, after);
|
|
122
|
+
let edited = false;
|
|
123
|
+
if (added.length > 0) {
|
|
124
|
+
const kept = new Set(after.map((i) => i.key));
|
|
125
|
+
const approval = await askHumanToAcceptCode(
|
|
126
|
+
server,
|
|
68
127
|
{
|
|
69
|
-
|
|
70
|
-
|
|
128
|
+
action: "update",
|
|
129
|
+
ruleType: saved.ruleType,
|
|
130
|
+
name: String(body.name ?? ruleId),
|
|
131
|
+
status: String(body.status ?? "Inactive"),
|
|
132
|
+
added,
|
|
133
|
+
replaced: before.filter((i) => !kept.has(i.key)),
|
|
71
134
|
},
|
|
72
|
-
|
|
73
|
-
|
|
135
|
+
{ relatedRequestId: extra?.requestId, signal: extra?.signal }
|
|
136
|
+
);
|
|
137
|
+
if (!approval.approved) {
|
|
138
|
+
return { content: [{ type: "text", text: codeNotApprovedText(`rule ${ruleId}`, approval.reason) }] };
|
|
139
|
+
}
|
|
140
|
+
// What the user accepted, including any edits they made in the review file.
|
|
141
|
+
body.pairs = applyAccepted(saved.ruleType, body.pairs, added, approval.accepted);
|
|
142
|
+
edited = approval.edited;
|
|
143
|
+
// The prompt can stay open for minutes. If the rule changed meanwhile, the
|
|
144
|
+
// user approved against a version that is no longer the saved one, and the
|
|
145
|
+
// merged body would overwrite the newer change: save nothing. (This narrows
|
|
146
|
+
// the window but can't close it: that needs a precondition on api2's
|
|
147
|
+
// update, which it doesn't offer.)
|
|
148
|
+
const again = await fetch(buildResourceUrl("rules", ruleId), { method: "GET", headers });
|
|
149
|
+
if (!again.ok) {
|
|
150
|
+
return await apiErrorResult("re-read the rule before saving it", again);
|
|
151
|
+
}
|
|
152
|
+
const againRead = await readApiJson("re-read the rule before saving it", again);
|
|
153
|
+
if (!againRead.ok) return againRead.result;
|
|
154
|
+
const againJson: any = againRead.data;
|
|
155
|
+
if (ruleVersion(againJson?.data ?? againJson) !== ruleVersion(saved)) {
|
|
156
|
+
return {
|
|
157
|
+
content: [
|
|
158
|
+
{
|
|
159
|
+
type: "text",
|
|
160
|
+
text:
|
|
161
|
+
`Not saved: rule ${ruleId} changed while the approval prompt was open, so the code the user ` +
|
|
162
|
+
`reviewed no longer matches what is saved. Nothing was changed. Do not retry unless the user asks again.`,
|
|
163
|
+
},
|
|
164
|
+
],
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const response = await fetch(buildResourceUrl("rules", ruleId), {
|
|
170
|
+
method: "PUT",
|
|
171
|
+
headers,
|
|
172
|
+
body: JSON.stringify(body),
|
|
173
|
+
});
|
|
174
|
+
if (!response.ok) {
|
|
175
|
+
// RQ-3025: status only — never reflect the upstream body into LLM context.
|
|
176
|
+
return await apiErrorResult("update rule", response);
|
|
177
|
+
}
|
|
178
|
+
const parsed = await readApiJson("update rule", response);
|
|
179
|
+
if (!parsed.ok) return parsed.result;
|
|
180
|
+
return untrustedDataResult("update_rule", parsed.data, edited ? EDITED_NOTE : "");
|
|
74
181
|
} catch (error) {
|
|
75
182
|
return {
|
|
76
183
|
content: [
|
|
@@ -12,8 +12,11 @@ function createRuleSchema<T extends z.infer<typeof RuleTypeEnum>>(
|
|
|
12
12
|
status: z
|
|
13
13
|
.enum(['Active', 'Inactive'])
|
|
14
14
|
.optional()
|
|
15
|
-
.default('
|
|
16
|
-
.describe(
|
|
15
|
+
.default('Inactive')
|
|
16
|
+
.describe(
|
|
17
|
+
'Status of the rule. Defaults to Inactive; rules must be explicitly activated ' +
|
|
18
|
+
'after a human reviews them. Script/Request/Response rules are always created Inactive.'
|
|
19
|
+
),
|
|
17
20
|
pairs: z
|
|
18
21
|
.array(pairSchema)
|
|
19
22
|
.describe('List of rule pair objects for the rule type.'),
|
|
@@ -49,8 +52,11 @@ const createMCPCompatibleSchema = () => {
|
|
|
49
52
|
status: z
|
|
50
53
|
.enum(['Active', 'Inactive'])
|
|
51
54
|
.optional()
|
|
52
|
-
.default('
|
|
53
|
-
.describe(
|
|
55
|
+
.default('Inactive')
|
|
56
|
+
.describe(
|
|
57
|
+
'Status of the rule. Defaults to Inactive; rules must be explicitly activated ' +
|
|
58
|
+
'after a human reviews them. Script/Request/Response rules are always created Inactive.'
|
|
59
|
+
),
|
|
54
60
|
groupId: z
|
|
55
61
|
.string()
|
|
56
62
|
.optional()
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import z from 'zod';
|
|
2
|
+
import { resourceIdSchema } from '../apiClient.js';
|
|
2
3
|
import {
|
|
3
4
|
CancelPairSchema,
|
|
4
5
|
DelayPairSchema,
|
|
@@ -19,15 +20,14 @@ function updateRuleSchema<T extends z.infer<typeof RuleTypeEnum>>(
|
|
|
19
20
|
pairSchema: z.ZodType<any>
|
|
20
21
|
) {
|
|
21
22
|
return z.object({
|
|
22
|
-
ruleId:
|
|
23
|
+
ruleId: resourceIdSchema.describe('Unique identifier for the rule.').optional(),
|
|
23
24
|
name: z.string().describe('Name of the rule.').optional(),
|
|
24
25
|
description: z.string().optional().describe('Description of the rule.'),
|
|
25
26
|
ruleType: z.literal(ruleType).describe('Type of the rule.'),
|
|
26
27
|
status: z
|
|
27
28
|
.enum(['Active', 'Inactive'])
|
|
28
29
|
.optional()
|
|
29
|
-
.
|
|
30
|
-
.describe('Status of the rule.'),
|
|
30
|
+
.describe('Status of the rule. Leave out to keep the current status.'),
|
|
31
31
|
pairs: z
|
|
32
32
|
.array(pairSchema)
|
|
33
33
|
.optional()
|
|
@@ -42,7 +42,7 @@ function updateRuleSchema<T extends z.infer<typeof RuleTypeEnum>>(
|
|
|
42
42
|
const createMCPCompatibleSchema = () => {
|
|
43
43
|
// Base fields that are common to all rules
|
|
44
44
|
const baseFields = {
|
|
45
|
-
ruleId:
|
|
45
|
+
ruleId: resourceIdSchema.describe('Unique identifier for the rule.').optional(),
|
|
46
46
|
name: z.string().describe('Name of the rule.').optional(),
|
|
47
47
|
description: z.string().optional().describe('Description of the rule.'),
|
|
48
48
|
ruleType: z
|
|
@@ -64,8 +64,7 @@ const createMCPCompatibleSchema = () => {
|
|
|
64
64
|
status: z
|
|
65
65
|
.enum(['Active', 'Inactive'])
|
|
66
66
|
.optional()
|
|
67
|
-
.
|
|
68
|
-
.describe('Status of the rule.'),
|
|
67
|
+
.describe('Status of the rule. Leave out to keep the current status.'),
|
|
69
68
|
objectType: z
|
|
70
69
|
.literal('rule')
|
|
71
70
|
.describe('Type of the object, always "rule".').default("rule"),
|
|
@@ -97,8 +96,8 @@ const createMCPCompatibleSchema = () => {
|
|
|
97
96
|
|
|
98
97
|
return {
|
|
99
98
|
...baseFields,
|
|
100
|
-
pairs: z.array(z.unknown()).describe(
|
|
101
|
-
`List of rule pair objects. Structure depends on ruleType:
|
|
99
|
+
pairs: z.array(z.unknown()).optional().describe(
|
|
100
|
+
`Leave out to keep the saved pairs. List of rule pair objects. Structure depends on ruleType:
|
|
102
101
|
|
|
103
102
|
- Redirect: ${pairDescriptions.Redirect}
|
|
104
103
|
- Cancel: ${pairDescriptions.Cancel}
|