post_to_social_media
Schedule and publish content to Facebook and Instagram with optional images and hashtags for social media marketing.
Instructions
Post content to Facebook and/or Instagram. Requires Meta API configuration. Can schedule posts for future publishing.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| hashtags | No | Optional: Comma-separated hashtags (without # symbol) | |
| imageUrl | No | Optional: URL to image (required for Instagram) | |
| message | Yes | Post caption/message | |
| platforms | Yes | Comma-separated platforms: facebook, instagram, or both | |
| scheduleTime | No | Optional: Schedule for future (YYYY-MM-DD HH:MM format) |
Implementation Reference
- src/index.ts:1086-1139 (handler)Handler function for 'post_to_social_media' tool. Checks configuration, parses input, calls appropriate platform posters (Facebook/Instagram), collects results, and returns formatted response with success/failure status.case 'post_to_social_media': { if (!socialMediaManager.isConfigured()) { return { content: [{ type: 'text', text: `⚠️ Social media not configured yet!\n\nTo use this feature:\n1. Create a Facebook Business account\n2. Create a Facebook App\n3. Get your access tokens\n4. Add them to your .env file\n\nSee FACEBOOK_INSTAGRAM_SETUP.md for detailed instructions.` }] }; } const platforms = String(args?.platforms || 'facebook').toLowerCase().split(',').map(p => p.trim()); const message = String(args?.message || ''); const imageUrl = args?.imageUrl ? String(args.imageUrl) : undefined; const hashtags = args?.hashtags ? String(args.hashtags).split(',').map(h => h.trim()) : undefined; const scheduleTime = args?.scheduleTime ? String(args.scheduleTime) : undefined; const results = []; for (const platform of platforms) { if (platform === 'facebook' || platform === 'both') { const result = await socialMediaManager.postToFacebook({ platform: 'facebook', message, imageUrl, scheduledTime: scheduleTime }); results.push(result); } if (platform === 'instagram' || platform === 'both') { const result = await socialMediaManager.postToInstagram({ platform: 'instagram', message, imageUrl, hashtags }); results.push(result); } } const successCount = results.filter(r => r.success).length; const failedResults = results.filter(r => !r.success); return { content: [{ type: 'text', text: `📱 Social Media Post Results\n\n✅ ${successCount}/${results.length} successful\n\n${results.map(r => r.success ? `✓ ${r.platform}: Posted${r.scheduledTime ? ` (scheduled for ${r.scheduledTime})` : ''}\n Post ID: ${r.postId}` : `✗ ${r.platform}: Failed - ${r.error}` ).join('\n\n')}${failedResults.length > 0 ? '\n\n💡 Tip: Check your API tokens and permissions if posts failed.' : ''}` }] }; }
- src/index.ts:362-375 (schema)Tool schema definition including name, description, and inputSchema for validation in MCP server.name: 'post_to_social_media', description: 'Post content to Facebook and/or Instagram. Requires Meta API configuration. Can schedule posts for future publishing.', inputSchema: { type: 'object', properties: { platforms: { type: 'string', description: 'Comma-separated platforms: facebook, instagram, or both' }, message: { type: 'string', description: 'Post caption/message' }, imageUrl: { type: 'string', description: 'Optional: URL to image (required for Instagram)' }, hashtags: { type: 'string', description: 'Optional: Comma-separated hashtags (without # symbol)' }, scheduleTime: { type: 'string', description: 'Optional: Schedule for future (YYYY-MM-DD HH:MM format)' }, }, required: ['platforms', 'message'], }, },
- src/index.ts:516-518 (registration)Registration of tool list handler, which returns the full tools array including 'post_to_social_media'.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools }; });
- src/social-media.ts:76-131 (helper)postToFacebook helper method: Posts to Facebook Graph API, handles scheduling, returns post result or error.async postToFacebook(post: SocialPost): Promise<PostResult> { if (!this.isConfigured()) { return { success: false, platform: 'facebook', error: 'Facebook API not configured. Please set FB_PAGE_TOKEN and FB_PAGE_ID environment variables.' }; } try { const url = `https://graph.facebook.com/${this.apiVersion}/${this.fbPageId}/feed`; const body: any = { message: post.message, access_token: this.fbPageToken, }; if (post.link) { body.link = post.link; } if (post.scheduledTime) { body.published = false; body.scheduled_publish_time = Math.floor(new Date(post.scheduledTime).getTime() / 1000); } const response = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }); const result = await response.json() as any; if (result.error) { return { success: false, platform: 'facebook', error: result.error.message }; } return { success: true, postId: result.id, platform: 'facebook', scheduledTime: post.scheduledTime }; } catch (error: any) { return { success: false, platform: 'facebook', error: error.message }; } }
- src/social-media.ts:136-222 (helper)postToInstagram helper method: Creates media container then publishes via Instagram Graph API, handles hashtags and images, returns post result or error.async postToInstagram(post: SocialPost): Promise<PostResult> { if (!this.isConfigured() || !this.igAccountId) { return { success: false, platform: 'instagram', error: 'Instagram API not configured. Please set IG_ACCOUNT_ID environment variable.' }; } if (!post.imageUrl) { return { success: false, platform: 'instagram', error: 'Instagram posts require an image. Please provide imageUrl.' }; } try { // Step 1: Create media container const containerUrl = `https://graph.facebook.com/${this.apiVersion}/${this.igAccountId}/media`; let caption = post.message; if (post.hashtags && post.hashtags.length > 0) { caption += '\n\n' + post.hashtags.map(tag => tag.startsWith('#') ? tag : `#${tag}`).join(' '); } const containerBody: any = { image_url: post.imageUrl, caption: caption, access_token: this.fbPageToken }; const containerResponse = await fetch(containerUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(containerBody) }); const containerResult = await containerResponse.json() as any; if (containerResult.error) { return { success: false, platform: 'instagram', error: containerResult.error.message }; } const containerId = containerResult.id; // Step 2: Publish container const publishUrl = `https://graph.facebook.com/${this.apiVersion}/${this.igAccountId}/media_publish`; const publishBody = { creation_id: containerId, access_token: this.fbPageToken }; const publishResponse = await fetch(publishUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(publishBody) }); const publishResult = await publishResponse.json() as any; if (publishResult.error) { return { success: false, platform: 'instagram', error: publishResult.error.message }; } return { success: true, postId: publishResult.id, platform: 'instagram' }; } catch (error: any) { return { success: false, platform: 'instagram', error: error.message }; } }