followUser
Follow Twitter users by their username to stay updated with their content and interactions.
Instructions
Follow a user by their username
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| username | Yes | The username of the user to follow |
Implementation Reference
- src/handlers/user.handlers.ts:60-84 (handler)The core handler function that implements the followUser tool logic. It retrieves the authenticated user's ID, looks up the target user by username, and calls the Twitter API v2 follow endpoint.export const handleFollowUser: TwitterHandler<UserHandlerArgs> = async ( client: TwitterClient | null, { username }: UserHandlerArgs ): Promise<HandlerResponse> => { if (!client) { return createMissingTwitterApiKeyResponse('followUser'); } try { const userId = await client.v2.me().then((response: any) => response.data.id); const targetUser = await client.v2.userByUsername(username); if (!targetUser.data) { throw new Error(`User not found: ${username}`); } await client.v2.follow(userId, targetUser.data.id); return createResponse(`Successfully followed user: ${username}`); } catch (error) { if (error instanceof Error) { throw new Error(formatTwitterError(error, 'following user')); } throw error; } };
- src/tools.ts:253-262 (registration)The tool registration definition in the TOOLS object, including description and input schema for the MCP server.followUser: { description: 'Follow a user by their username', inputSchema: { type: 'object', properties: { username: { type: 'string', description: 'The username of the user to follow' } }, required: ['username'], }, },
- src/types.ts:69-71 (schema)TypeScript interface defining the input arguments for the followUser tool.export interface FollowUserArgs { username: string; }
- src/types.ts:334-341 (schema)Runtime validation function that asserts the arguments match FollowUserArgs interface.export function assertFollowUserArgs(args: unknown): asserts args is FollowUserArgs { if (typeof args !== 'object' || args === null) { throw new Error('Invalid arguments: expected object'); } if (!('username' in args) || typeof (args as any).username !== 'string') { throw new Error('Invalid arguments: expected username string'); } }
- src/index.ts:260-263 (registration)The switch case in the main CallToolRequestSchema handler that routes 'followUser' calls to the handleFollowUser function.case 'followUser': { const { username } = request.params.arguments as { username: string }; response = await handleFollowUser(client, { username }); break;