followUser
Follow a Twitter user by their username using this tool, enabling direct interactions within the Twitter MCP Server for enhanced social engagement.
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)Core implementation of the followUser tool. Retrieves the authenticated user's ID, looks up the target user by username, and uses the Twitter API v2 follow method to establish the follow relationship.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 (schema)MCP tool schema definition including description and input validation schema requiring a 'username' string parameter.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/index.ts:260-263 (registration)Registration and dispatch logic in the main MCP server request handler that routes 'followUser' tool calls to the specific handleFollowUser function.case 'followUser': { const { username } = request.params.arguments as { username: string }; response = await handleFollowUser(client, { username }); break;
- src/types.ts:69-71 (schema)TypeScript interface defining the expected arguments for the followUser handler (username: string).export interface FollowUserArgs { username: string; }
- src/index.ts:33-39 (registration)Import statement registering the handleFollowUser function for use in the MCP tool dispatch switch.handleGetUserInfo, handleFollowUser, handleUnfollowUser, handleGetFollowers, handleGetFollowing, handleGetAuthenticatedUser } from './handlers/user.handlers.js';