File size: 1,792 Bytes
b39461a
 
796ba8e
f509db9
dd125ca
076e59d
a65d39f
b39461a
 
 
 
 
4975c44
b39461a
 
 
 
 
 
 
f509db9
796ba8e
 
 
b39461a
f509db9
 
 
 
b39461a
f509db9
b39461a
f509db9
b39461a
62cf129
dd125ca
b39461a
 
8397cce
b39461a
 
 
8397cce
b39461a
 
 
 
 
 
 
 
 
 
 
 
d034133
b39461a
 
a65d39f
d034133
b39461a
 
 
 
 
 
 
a65d39f
b39461a
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { type InferSchema, type ToolMetadata } from "xmcp";
import { headers } from "xmcp/headers";
import { z } from "zod";
import { userResourceSchema } from "../schemas/userResourceSchema";
import { getScimBaseUrl } from "../utils/getSCIMBaseUrl";
import { getScimToken } from "../utils/getSCIMToken";
import { readJsonBody } from "../utils/responseBody";

export const metadata: ToolMetadata = {
  name: "create-user",
  description: "Create a SCIM user resource",
  annotations: {
    title: "Create User Resource",
    readOnlyHint: false,
    destructiveHint: false,
    idempotentHint: true,
    openWorldHint: true,
  },
};

const createUserInputSchema = z.object(userResourceSchema).omit({
  id: true,
  meta: true,
});

export const schema = {
  userResource: createUserInputSchema,
};

export default async function createUser(
  params: InferSchema<typeof schema>
) {
  const { userResource } = params;
  const requestHeaders = headers();
  const apiToken = getScimToken(requestHeaders);
  const baseUrl = getScimBaseUrl(requestHeaders);

  if (!apiToken) {
    throw new Error("Missing required headers: x-scim-api-token or SCIM_API_TOKEN env");
  }

  if (!baseUrl) {
    throw new Error("Missing required headers: x-scim-base-url or SCIM_API_BASE_URL env");
  }

  const response = await fetch(`${baseUrl}/Users`, {
    method: "POST",
    headers: {
      "Content-Type": "application/scim+json",
      Authorization: `Bearer ${apiToken}`,
    },
    body: JSON.stringify(userResource),
  });

  if (!response.ok) {
    throw new Error(await response.text());
  }

  const data = await readJsonBody(response);

  return {
    content: [
      {
        type: "text",
        text: `User created successfully`,
      },
    ],
    structuredContent: data ?? undefined,
  };
}