feat(mcp): add credential CRUD tools, bump to 1.13.0
- add list/get/create/update/delete_credential MCP tools so credentials can be created and attached to scenarios entirely via MCP - update_credential only changes the fields passed, keeping data intact - add changelog entries for 1.13.0 and the missing 1.12.0 release
This commit is contained in:
1 parent
d99bb75fc2
commit
5ea1e38f25
5 files changed
+318
-3
No files matched your search
@@ -275,6 +275,166 @@ export class McpService {
|
||||
},
|
||||
);
|
||||
|
||||
// ── Credentials ───────────────────────────────────────────────────────────
|
||||
|
||||
server.registerTool(
|
||||
"list_credentials",
|
||||
{
|
||||
description: "List all credentials (paginated)",
|
||||
inputSchema: {
|
||||
page: z
|
||||
.number()
|
||||
.int()
|
||||
.min(1)
|
||||
.optional()
|
||||
.describe("Page number (default 1)"),
|
||||
limit: z
|
||||
.number()
|
||||
.int()
|
||||
.min(1)
|
||||
.optional()
|
||||
.describe("Items per page (default 20)"),
|
||||
orderBy: z
|
||||
.enum(["id", "name", "lastUsedAt", "createdAt", "updatedAt"])
|
||||
.optional()
|
||||
.describe("Field to order by (default id)"),
|
||||
orderDir: z
|
||||
.enum(["ASC", "DESC"])
|
||||
.optional()
|
||||
.describe("Sort direction (default ASC)"),
|
||||
},
|
||||
},
|
||||
async ({ page, limit, orderBy, orderDir }) => {
|
||||
const result = await this.credentialService.findAll({
|
||||
page,
|
||||
limit,
|
||||
orderBy,
|
||||
orderDir,
|
||||
});
|
||||
return {
|
||||
content: [{ type: "text" as const, text: JSON.stringify(result) }],
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
"get_credential",
|
||||
{
|
||||
description: "Get a credential by ID",
|
||||
inputSchema: {
|
||||
id: z.uuid().describe("Credential ID"),
|
||||
},
|
||||
},
|
||||
async ({ id }) => {
|
||||
try {
|
||||
const credential = await this.credentialService.findOne(id);
|
||||
return {
|
||||
content: [
|
||||
{ type: "text" as const, text: JSON.stringify(credential) },
|
||||
],
|
||||
};
|
||||
} catch (err) {
|
||||
return {
|
||||
isError: true,
|
||||
content: [{ type: "text" as const, text: (err as Error).message }],
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
"create_credential",
|
||||
{
|
||||
description:
|
||||
"Create a new credential. Assign it to a scenario with add_scenario_credential to use it via context.getCredential(alias).",
|
||||
inputSchema: {
|
||||
name: z.string().min(1).describe("Human-readable credential name"),
|
||||
data: z
|
||||
.string()
|
||||
.optional()
|
||||
.describe(
|
||||
'JSON-encoded credential payload, e.g. {"token":"abc123"}',
|
||||
),
|
||||
},
|
||||
},
|
||||
async ({ name, data }) => {
|
||||
try {
|
||||
const credential = await this.credentialService.create({
|
||||
name,
|
||||
data,
|
||||
});
|
||||
return {
|
||||
content: [
|
||||
{ type: "text" as const, text: JSON.stringify(credential) },
|
||||
],
|
||||
};
|
||||
} catch (err) {
|
||||
return {
|
||||
isError: true,
|
||||
content: [{ type: "text" as const, text: (err as Error).message }],
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
"update_credential",
|
||||
{
|
||||
description: "Update an existing credential (name and/or data)",
|
||||
inputSchema: {
|
||||
id: z.uuid().describe("Credential ID to update"),
|
||||
name: z.string().min(1).optional().describe("New name"),
|
||||
data: z
|
||||
.string()
|
||||
.optional()
|
||||
.describe("New JSON-encoded credential payload"),
|
||||
},
|
||||
},
|
||||
async ({ id, name, data }) => {
|
||||
try {
|
||||
const credential = await this.credentialService.update(id, {
|
||||
...(name !== undefined && { name }),
|
||||
...(data !== undefined && { data }),
|
||||
});
|
||||
return {
|
||||
content: [
|
||||
{ type: "text" as const, text: JSON.stringify(credential) },
|
||||
],
|
||||
};
|
||||
} catch (err) {
|
||||
return {
|
||||
isError: true,
|
||||
content: [{ type: "text" as const, text: (err as Error).message }],
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
"delete_credential",
|
||||
{
|
||||
description: "Delete a credential by ID",
|
||||
inputSchema: {
|
||||
id: z.uuid().describe("Credential ID to delete"),
|
||||
},
|
||||
},
|
||||
async ({ id }) => {
|
||||
try {
|
||||
await this.credentialService.remove(id);
|
||||
return {
|
||||
content: [
|
||||
{ type: "text" as const, text: `Credential ${id} deleted` },
|
||||
],
|
||||
};
|
||||
} catch (err) {
|
||||
return {
|
||||
isError: true,
|
||||
content: [{ type: "text" as const, text: (err as Error).message }],
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
// ── Browser ───────────────────────────────────────────────────────────────
|
||||
|
||||
server.registerTool(
|
||||
|
||||
@@ -843,4 +843,143 @@ describe("McpController", () => {
|
||||
expect(result.isError).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
// ── Credential CRUD tools ─────────────────────────────────────────────────
|
||||
|
||||
async function createCredential(name: string, data?: string) {
|
||||
const { rpc } = await mcpCall("create_credential", { name, data });
|
||||
return JSON.parse(
|
||||
(rpc.result as { content: { text: string }[] }).content[0].text,
|
||||
) as { id: string; name: string; data: string | null };
|
||||
}
|
||||
|
||||
describe("list_credentials", () => {
|
||||
it("returns a paginated result with a data array", async () => {
|
||||
await createCredential("mcp-list-cred");
|
||||
const { status, rpc } = await mcpCall("list_credentials", {
|
||||
limit: 100,
|
||||
});
|
||||
expect(status).toBe(200);
|
||||
const result = rpc.result as { content: { text: string }[] };
|
||||
const body = JSON.parse(result.content[0].text) as {
|
||||
data: { name: string }[];
|
||||
total: number;
|
||||
};
|
||||
expect(Array.isArray(body.data)).toBe(true);
|
||||
expect(typeof body.total).toBe("number");
|
||||
expect(body.data.some((c) => c.name === "mcp-list-cred")).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("create_credential", () => {
|
||||
it("creates a credential and returns it with name and data", async () => {
|
||||
const created = await createCredential(
|
||||
"mcp-test-cred",
|
||||
'{"token":"abc123"}',
|
||||
);
|
||||
expect(typeof created.id).toBe("string");
|
||||
expect(created.name).toBe("mcp-test-cred");
|
||||
expect(created.data).toBe('{"token":"abc123"}');
|
||||
});
|
||||
|
||||
it("stores null data when data is omitted", async () => {
|
||||
const created = await createCredential("mcp-test-cred-nodata");
|
||||
expect(created.data).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("get_credential", () => {
|
||||
it("returns the created credential by id", async () => {
|
||||
const created = await createCredential("mcp-get-cred", '{"a":1}');
|
||||
const { status, rpc } = await mcpCall("get_credential", {
|
||||
id: created.id,
|
||||
});
|
||||
expect(status).toBe(200);
|
||||
const result = rpc.result as { content: { text: string }[] };
|
||||
const fetched = JSON.parse(result.content[0].text) as {
|
||||
id: string;
|
||||
name: string;
|
||||
data: string;
|
||||
};
|
||||
expect(fetched.id).toBe(created.id);
|
||||
expect(fetched.name).toBe("mcp-get-cred");
|
||||
expect(fetched.data).toBe('{"a":1}');
|
||||
});
|
||||
|
||||
it("returns an MCP error for a non-existent credential id", async () => {
|
||||
const { status, rpc } = await mcpCall("get_credential", {
|
||||
id: "00000000-0000-0000-0000-000000000000",
|
||||
});
|
||||
expect(status).toBe(200);
|
||||
const result = rpc.result as { isError: boolean };
|
||||
expect(result.isError).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("update_credential", () => {
|
||||
it("updates name and data of an existing credential", async () => {
|
||||
const created = await createCredential("mcp-upd-cred-orig", '{"v":1}');
|
||||
const { status, rpc } = await mcpCall("update_credential", {
|
||||
id: created.id,
|
||||
name: "mcp-upd-cred-new",
|
||||
data: '{"v":2}',
|
||||
});
|
||||
expect(status).toBe(200);
|
||||
const result = rpc.result as { content: { text: string }[] };
|
||||
const updated = JSON.parse(result.content[0].text) as {
|
||||
name: string;
|
||||
data: string;
|
||||
};
|
||||
expect(updated.name).toBe("mcp-upd-cred-new");
|
||||
expect(updated.data).toBe('{"v":2}');
|
||||
});
|
||||
|
||||
it("leaves data untouched when only name is updated", async () => {
|
||||
const created = await createCredential("mcp-upd-cred-partial", '{"k":1}');
|
||||
const { rpc } = await mcpCall("update_credential", {
|
||||
id: created.id,
|
||||
name: "mcp-upd-cred-partial-new",
|
||||
});
|
||||
const updated = JSON.parse(
|
||||
(rpc.result as { content: { text: string }[] }).content[0].text,
|
||||
) as { name: string; data: string };
|
||||
expect(updated.name).toBe("mcp-upd-cred-partial-new");
|
||||
expect(updated.data).toBe('{"k":1}');
|
||||
});
|
||||
|
||||
it("returns an MCP error for a non-existent credential id", async () => {
|
||||
const { status, rpc } = await mcpCall("update_credential", {
|
||||
id: "00000000-0000-0000-0000-000000000000",
|
||||
name: "Ghost",
|
||||
});
|
||||
expect(status).toBe(200);
|
||||
const result = rpc.result as { isError: boolean };
|
||||
expect(result.isError).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("delete_credential", () => {
|
||||
it("deletes an existing credential and confirms deletion", async () => {
|
||||
const created = await createCredential("mcp-del-cred");
|
||||
const { status, rpc } = await mcpCall("delete_credential", {
|
||||
id: created.id,
|
||||
});
|
||||
expect(status).toBe(200);
|
||||
const result = rpc.result as { content: { text: string }[] };
|
||||
expect(result.content[0].text).toContain(created.id);
|
||||
|
||||
const getResult = (await mcpCall("get_credential", { id: created.id }))
|
||||
.rpc.result as { isError: boolean };
|
||||
expect(getResult.isError).toBe(true);
|
||||
});
|
||||
|
||||
it("returns an MCP error for a non-existent credential id", async () => {
|
||||
const { status, rpc } = await mcpCall("delete_credential", {
|
||||
id: "00000000-0000-0000-0000-000000000000",
|
||||
});
|
||||
expect(status).toBe(200);
|
||||
const result = rpc.result as { isError: boolean };
|
||||
expect(result.isError).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user