blockUser
Block a user on Twitter by user ID or username to restrict them from following you or viewing your tweets. Use this tool for enhanced privacy and account control.
Instructions
Block a user account to prevent them from following you or viewing your tweets
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| userId | No | The ID of the user to block | |
| username | No | The username of the user to block (alternative to userId) |
Implementation Reference
- The core handler function that implements the blockUser tool logic using Twitter API v2 to block a user by ID or username.export const handleBlockUser: TwitterHandler<BlockUserArgs> = async ( client: TwitterClient | null, { userId, username }: BlockUserArgs ): Promise<HandlerResponse> => { if (!client) { return createMissingTwitterApiKeyResponse('blockUser'); } try { if (!userId && !username) { throw new Error('Either userId or username must be provided'); } let targetUserId = userId; // If username provided, get the user ID first if (username && !userId) { const userResponse = await client.v2.userByUsername(username); if (!userResponse.data) { throw new Error(`User with username '${username}' not found`); } targetUserId = userResponse.data.id; } // Get authenticated user's ID const me = await client.v2.me(); const myUserId = me.data.id; // Block the user const result = await client.v2.block(myUserId, targetUserId!); return createResponse(`Successfully blocked user ${username || targetUserId}. Response: ${JSON.stringify(result, null, 2)}`); } catch (error) { if (error instanceof Error) { throw new Error(formatTwitterError(error, 'blocking user')); } throw error; } };
- src/types/handlers.ts:115-118 (schema)TypeScript interface defining the input arguments for the blockUser handler (userId or username).export interface BlockUserArgs { userId?: string; username?: string; }
- src/tools.ts:613-629 (schema)JSON schema definition for the blockUser tool input, used for MCP tool registration.blockUser: { description: 'Block a user account to prevent them from following you or viewing your tweets', inputSchema: { type: 'object', properties: { userId: { type: 'string', description: 'The ID of the user to block' }, username: { type: 'string', description: 'The username of the user to block (alternative to userId)' } }, required: [] } },
- src/index.ts:387-390 (registration)Dispatch/registration point in the main server handler switch statement where blockUser tool calls are routed to the handler.case 'blockUser': { const { userId, username } = request.params.arguments as { userId?: string; username?: string }; response = await handleBlockUser(client, { userId, username }); break;
- src/tools.ts:613-629 (registration)Tool metadata registration in the TOOLS object exported for MCP list tools endpoint.blockUser: { description: 'Block a user account to prevent them from following you or viewing your tweets', inputSchema: { type: 'object', properties: { userId: { type: 'string', description: 'The ID of the user to block' }, username: { type: 'string', description: 'The username of the user to block (alternative to userId)' } }, required: [] } },