Skip to content

Commit f3ee88d

Browse files
committed
feat: add users tools
1 parent 0ea29e0 commit f3ee88d

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

notion/src/index.ts

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ interface UpdatePagePropertiesArgs {
3939
properties: any;
4040
}
4141

42+
// Users
43+
interface ListAllUsersArgs {
44+
start_cursor?: string;
45+
page_size?: number;
46+
}
47+
48+
interface RetrieveUserArgs {
49+
user_id: string;
50+
}
51+
4252
// Databases
4353
interface CreateDatabaseArgs {
4454
parent: any;
@@ -198,6 +208,53 @@ const updatePagePropertiesTool: Tool = {
198208
},
199209
};
200210

211+
// Users
212+
const listAllUsersTool: Tool = {
213+
name: "notion_list_all_users",
214+
description:
215+
"List all users in the Notion workspace. **Note:** This function requires upgrading to the Notion Enterprise plan and using an Organization API key to avoid permission errors.",
216+
inputSchema: {
217+
type: "object",
218+
properties: {
219+
start_cursor: {
220+
type: "string",
221+
description: "Pagination start cursor for listing users",
222+
},
223+
page_size: {
224+
type: "number",
225+
description: "Number of users to retrieve (max 100)",
226+
},
227+
},
228+
},
229+
};
230+
231+
const retrieveUserTool: Tool = {
232+
name: "notion_retrieve_user",
233+
description:
234+
"Retrieve a specific user by user_id in Notion. **Note:** This function requires upgrading to the Notion Enterprise plan and using an Organization API key to avoid permission errors.",
235+
inputSchema: {
236+
type: "object",
237+
properties: {
238+
user_id: {
239+
type: "string",
240+
description:
241+
"The ID of the user to retrieve. It should be a 32-character string (excluding hyphens) formatted as 8-4-4-4-12 with hyphens (-).",
242+
},
243+
},
244+
required: ["user_id"],
245+
},
246+
};
247+
248+
const retrieveBotUserTool: Tool = {
249+
name: "notion_retrieve_bot_user",
250+
description:
251+
"Retrieve the bot user associated with the current token in Notion",
252+
inputSchema: {
253+
type: "object",
254+
properties: {},
255+
},
256+
};
257+
201258
// Databases
202259
const createDatabaseTool: Tool = {
203260
name: "notion_create_database",
@@ -324,6 +381,7 @@ const createDatabaseItemTool: Tool = {
324381
},
325382
};
326383

384+
// Search
327385
const searchTool: Tool = {
328386
name: "notion_search",
329387
description: "Search pages or databases by title in Notion",
@@ -462,6 +520,34 @@ class NotionClientWrapper {
462520
return response.json();
463521
}
464522

523+
async listAllUsers(start_cursor?: string, page_size?: number): Promise<any> {
524+
const params = new URLSearchParams();
525+
if (start_cursor) params.append("start_cursor", start_cursor);
526+
if (page_size) params.append("page_size", page_size.toString());
527+
528+
const response = await fetch(`${this.baseUrl}/users?${params.toString()}`, {
529+
method: "GET",
530+
headers: this.headers,
531+
});
532+
return response.json();
533+
}
534+
535+
async retrieveUser(user_id: string): Promise<any> {
536+
const response = await fetch(`${this.baseUrl}/users/${user_id}`, {
537+
method: "GET",
538+
headers: this.headers,
539+
});
540+
return response.json();
541+
}
542+
543+
async retrieveBotUser(): Promise<any> {
544+
const response = await fetch(`${this.baseUrl}/users/me`, {
545+
method: "GET",
546+
headers: this.headers,
547+
});
548+
return response.json();
549+
}
550+
465551
async createDatabase(
466552
parent: any,
467553
title: any[],
@@ -692,6 +778,37 @@ async function main() {
692778
};
693779
}
694780

781+
case "notion_list_all_users": {
782+
const args = request.params
783+
.arguments as unknown as ListAllUsersArgs;
784+
const response = await notionClient.listAllUsers(
785+
args.start_cursor,
786+
args.page_size
787+
);
788+
return {
789+
content: [{ type: "text", text: JSON.stringify(response) }],
790+
};
791+
}
792+
793+
case "notion_retrieve_user": {
794+
const args = request.params
795+
.arguments as unknown as RetrieveUserArgs;
796+
if (!args.user_id) {
797+
throw new Error("Missing required argument: user_id");
798+
}
799+
const response = await notionClient.retrieveUser(args.user_id);
800+
return {
801+
content: [{ type: "text", text: JSON.stringify(response) }],
802+
};
803+
}
804+
805+
case "notion_retrieve_bot_user": {
806+
const response = await notionClient.retrieveBotUser();
807+
return {
808+
content: [{ type: "text", text: JSON.stringify(response) }],
809+
};
810+
}
811+
695812
case "notion_query_database": {
696813
const args = request.params
697814
.arguments as unknown as QueryDatabaseArgs;
@@ -803,6 +920,9 @@ async function main() {
803920
deleteBlockTool,
804921
retrievePageTool,
805922
updatePagePropertiesTool,
923+
listAllUsersTool,
924+
retrieveUserTool,
925+
retrieveBotUserTool,
806926
createDatabaseTool,
807927
queryDatabaseTool,
808928
retrieveDatabaseTool,

0 commit comments

Comments
 (0)