import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
import { RipplingClient } from "../clients/rippling-client.js";
import { RipplingApiError } from "../utils/errors.js";
export function registerGroupTools(
server: McpServer,
client: RipplingClient
): void {
server.tool(
"list_groups",
"List all groups in the organization",
{},
async () => {
try {
const groups = await client.listGroups();
return {
content: [
{
type: "text",
text: JSON.stringify(groups, null, 2),
},
],
};
} catch (error) {
if (error instanceof RipplingApiError) return error.toToolResult();
throw error;
}
}
);
server.tool(
"create_group",
"Create a new group with specified members",
{
name: z.string().describe("Name for the new group"),
spokeId: z
.string()
.describe("Unique identifier for the group in your application"),
userIds: z
.array(z.string())
.describe("Array of user IDs to add as members"),
},
async ({ name, spokeId, userIds }) => {
try {
const group = await client.createGroup({ name, spokeId, userIds });
return {
content: [
{
type: "text",
text: `Group "${name}" created successfully.\n\n${JSON.stringify(group, null, 2)}`,
},
],
};
} catch (error) {
if (error instanceof RipplingApiError) return error.toToolResult();
throw error;
}
}
);
server.tool(
"update_group",
"Update an existing group's name or members",
{
groupId: z.string().describe("The group ID to update"),
name: z.string().optional().describe("New name for the group"),
userIds: z
.array(z.string())
.optional()
.describe("New array of user IDs (replaces existing members)"),
},
async ({ groupId, name, userIds }) => {
try {
const updates: Record<string, unknown> = {};
if (name !== undefined) updates.name = name;
if (userIds !== undefined) updates.userIds = userIds;
const group = await client.updateGroup(groupId, updates);
return {
content: [
{
type: "text",
text: `Group ${groupId} updated successfully.\n\n${JSON.stringify(group, null, 2)}`,
},
],
};
} catch (error) {
if (error instanceof RipplingApiError) return error.toToolResult();
throw error;
}
}
);
server.tool(
"delete_group",
"Delete a group by its ID",
{
groupId: z.string().describe("The group ID to delete"),
},
async ({ groupId }) => {
try {
await client.deleteGroup(groupId);
return {
content: [
{
type: "text",
text: `Group ${groupId} has been deleted.`,
},
],
};
} catch (error) {
if (error instanceof RipplingApiError) return error.toToolResult();
throw error;
}
}
);
}