get_user_info
Retrieve detailed information about a Bilibili user by providing their numeric ID for analysis or integration in workflows.
Instructions
Get information about a Bilibili user
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| mid | Yes | User's numeric ID |
Implementation Reference
- src/tools/user.ts:12-35 (handler)The tool handler function that executes the logic for 'get_user_info': fetches user data using helper, formats it, and returns as text content block, with error handling.async ({ mid }) => { try { const userInfo = await getUserInfo(mid) || {} const formattedInfo = formatUserInfo(userInfo) return { content: [ { type: "text", text: formattedInfo, }, ], } } catch (error) { return { content: [ { type: "text", text: `get user info failed: ${error instanceof Error ? error.message : String(error)}`, }, ], } } }
- src/tools/user.ts:9-11 (schema)Input schema using Zod for the 'mid' parameter (user numeric ID).{ mid: z.number().int().positive().describe("User's numeric ID"), },
- src/tools/user.ts:6-36 (registration)Registration of the 'get_user_info' tool on the MCP server, specifying name, description, input schema, and handler.server.tool( "get_user_info", "Get information about a Bilibili user", { mid: z.number().int().positive().describe("User's numeric ID"), }, async ({ mid }) => { try { const userInfo = await getUserInfo(mid) || {} const formattedInfo = formatUserInfo(userInfo) return { content: [ { type: "text", text: formattedInfo, }, ], } } catch (error) { return { content: [ { type: "text", text: `get user info failed: ${error instanceof Error ? error.message : String(error)}`, }, ], } } } )
- src/common/utils.ts:13-32 (helper)Helper function that retrieves and merges Bilibili user information (basic info + follow stats) from API.export async function getUserInfo(mid: number): Promise<UserInfo> { try { // 获取用户基本信息 const userInfo = (await userAPI.getInfo(mid)) || {} // 获取用户粉丝和关注数 const followData = (await userAPI.getRelationStat(mid)) || {} // 合并数据 userInfo.followInfo = { follower: followData.follower, following: followData.following, } return userInfo } catch (error) { console.error("Error fetching user info:", error) throw error } }
- src/common/utils.ts:99-136 (helper)Helper function that formats UserInfo into a readable multi-line string using internationalization.export function formatUserInfo(user: UserInfo): string { const t = i18n.user const baseInfo = { [t.profile]: `https://space.bilibili.com/${user.mid}`, [t.uid]: user.mid, } const optionalInfo: Record<string, string | undefined> = { [t.nickname]: user.name, [t.followers]: user.followInfo?.follower?.toLocaleString(), [t.following]: user.followInfo?.following?.toLocaleString(), [t.level]: user.level?.toString(), [t.avatar]: user.face, [t.bio]: user.sign, [t.birthday]: user.birthday, [t.tags]: user.tags?.length > 0 ? user.tags.join(", ") : undefined, [t.verification]: user.official?.title, [t.verificationDesc]: user.official?.title ? user.official?.desc : undefined, [t.liveRoomUrl]: user.live_room?.url, [t.liveStatus]: user.live_room?.url ? user.live_room.liveStatus ? t.liveOn : t.liveOff : undefined, } let info = Object.entries(baseInfo) .map(([key, value]) => `${key}: ${value}`) .join("\n") + "\n" info += Object.entries(optionalInfo) .filter(([_, value]) => value !== undefined && value !== "") .map(([key, value]) => `${key}: ${value}`) .join("\n") return info }