chenhunghan commited on
Commit
1afdaf6
·
unverified ·
1 Parent(s): c691d96

feat: add deleteUser and replaceUser tools

Browse files

Signed-off-by: Hung-Han (Henry) Chen <chenhungh@gmail.com>

Files changed (2) hide show
  1. src/tools/deleteUser.ts +58 -0
  2. src/tools/replaceUser.ts +67 -0
src/tools/deleteUser.ts ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { type InferSchema, type ToolMetadata } from "xmcp";
2
+ import { headers } from "xmcp/headers";
3
+ import { z } from "zod";
4
+
5
+ export const metadata: ToolMetadata = {
6
+ name: "delete-user",
7
+ description: "Delete a user resource",
8
+ annotations: {
9
+ title: "Delete User Resource",
10
+ readOnlyHint: false,
11
+ destructiveHint: true,
12
+ idempotentHint: true,
13
+ openWorldHint: true,
14
+ },
15
+ };
16
+
17
+ export const schema = {
18
+ userId: z.string().describe("The unique identifier of the user to delete"),
19
+ };
20
+
21
+ export default async function deleteUser(
22
+ params: InferSchema<typeof schema>
23
+ ) {
24
+ const requestHeaders = headers();
25
+ const apiToken = requestHeaders["x-scim-api-key"];
26
+ const baseUrl = requestHeaders["x-scim-base-url"];
27
+
28
+ if (!apiToken) {
29
+ throw new Error("Missing required headers: x-scim-api-key");
30
+ }
31
+
32
+ if (!baseUrl) {
33
+ throw new Error("Missing required headers: x-scim-base-url");
34
+ }
35
+
36
+ const { userId } = params;
37
+
38
+ const response = await fetch(`${baseUrl}/Users/${userId}`, {
39
+ method: "DELETE",
40
+ headers: {
41
+ "Content-Type": "application/scim+json",
42
+ Authorization: `Bearer ${apiToken}`,
43
+ },
44
+ });
45
+
46
+ if (!response.ok) {
47
+ throw new Error(await response.text());
48
+ }
49
+
50
+ return {
51
+ content: [
52
+ {
53
+ type: "text",
54
+ text: `User ${userId} deleted successfully`,
55
+ },
56
+ ],
57
+ };
58
+ }
src/tools/replaceUser.ts ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { type InferSchema, type ToolMetadata } from "xmcp";
2
+ import { headers } from "xmcp/headers";
3
+ import { userResourceSchema } from "../schemas/userResourceSchema";
4
+ import { z } from "zod";
5
+
6
+ export const metadata: ToolMetadata = {
7
+ name: "replace-user",
8
+ description: "Replace a user resource",
9
+ annotations: {
10
+ title: "Replace User Resource",
11
+ readOnlyHint: false,
12
+ destructiveHint: false,
13
+ idempotentHint: true,
14
+ openWorldHint: true,
15
+ },
16
+ };
17
+
18
+ export const schema = {
19
+ userId: z.string().describe("The unique identifier of the user to replace"),
20
+ ...userResourceSchema,
21
+ };
22
+
23
+ export default async function replaceUser(
24
+ params: InferSchema<typeof schema>
25
+ ) {
26
+ const requestHeaders = headers();
27
+ const apiToken = requestHeaders["x-scim-api-key"];
28
+ const baseUrl = requestHeaders["x-scim-base-url"];
29
+
30
+ if (!apiToken) {
31
+ throw new Error("Missing required headers: x-scim-api-key");
32
+ }
33
+
34
+ if (!baseUrl) {
35
+ throw new Error("Missing required headers: x-scim-base-url");
36
+ }
37
+
38
+ const { userId, ...userResource } = params;
39
+
40
+ const response = await fetch(`${baseUrl}/Users/${userId}`, {
41
+ method: "PUT",
42
+ headers: {
43
+ "Content-Type": "application/scim+json",
44
+ Authorization: `Bearer ${apiToken}`,
45
+ },
46
+ body: JSON.stringify(userResource),
47
+ });
48
+
49
+ if (!response.ok) {
50
+ throw new Error(await response.text());
51
+ }
52
+
53
+ return {
54
+ content: [
55
+ {
56
+ type: "text",
57
+ text: `User ${userId} replaced successfully`,
58
+ },
59
+ {
60
+ type: "resource_link",
61
+ name: "User resource",
62
+ uri: `users://${userId}`,
63
+ },
64
+ ],
65
+ structuredContent: await response.json(),
66
+ };
67
+ }