addUserToList
Add a specified user to a Twitter list by providing the list ID and the username, enabling organized user management directly through the Twitter MCP Server.
Instructions
Add a user to a Twitter list
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| listId | Yes | The ID of the list | |
| username | Yes | The username of the user to add |
Implementation Reference
- src/handlers/list.handlers.ts:132-178 (handler)The core handler function that implements the addUserToList tool logic: verifies user and list, adds member via Twitter API v2, handles errors.export async function handleAddUserToList( client: TwitterClient | null, args: AddUserToListArgs ): Promise<HandlerResponse> { if (!client) { return createMissingTwitterApiKeyResponse('addUserToList'); } try { // First, verify the user exists and get their username for the response message const user = await client.getUserById(args.userId); if (!user.data) { throw new Error(`User with ID ${args.userId} not found`); } // Then verify the list exists and get its name for the response message const list = await client.getList(args.listId); if (!list.data) { throw new Error(`List with ID ${args.listId} not found`); } // Now try to add the user to the list await client.addListMember(args.listId, args.userId); return createResponse( `Successfully added user @${user.data.username} to list "${list.data.name}"` ); } catch (error) { if (error instanceof ApiResponseError) { // Handle specific Twitter API errors if (error.rateLimitError && error.rateLimit) { const resetMinutes = Math.ceil(error.rateLimit.reset / 60); throw new Error(`Rate limit exceeded. Please try again in ${resetMinutes} minutes.`); } if (error.code === 403) { throw new Error('You do not have permission to add members to this list.'); } if (error.code === 404) { throw new Error('The specified user or list could not be found.'); } throw new Error(`Twitter API Error: ${error.message}`); } if (error instanceof Error) { throw new Error(formatTwitterError(error, 'adding user to list')); } throw new Error('Failed to add user to list: Unknown error occurred'); } }
- src/tools.ts:333-343 (schema)Defines the tool schema and description for MCP tool listing, including input validation schema (note: uses 'username' param).addUserToList: { description: 'Add a user to a Twitter list', inputSchema: { type: 'object', properties: { listId: { type: 'string', description: 'The ID of the list' }, username: { type: 'string', description: 'The username of the user to add' } }, required: ['listId', 'username'], }, },
- src/index.ts:289-292 (registration)Registers and dispatches the tool call in the main MCP server request handler switch statement.case 'addUserToList': { const { listId, userId } = request.params.arguments as { listId: string; userId: string }; response = await handleAddUserToList(client, { listId, userId }); break;
- src/handlers/list.handlers.ts:19-22 (schema)TypeScript interface defining the arguments expected by the handler function (uses 'userId').export interface AddUserToListArgs { listId: string; userId: string; }
- src/index.ts:41-46 (registration)Import statement registering the handler function for use in the main server.handleCreateList, handleAddUserToList, handleRemoveUserFromList, handleGetListMembers, handleGetUserLists } from './handlers/list.handlers.js';