Skip to main content
Glama
posts.ts2.23 kB
import { z } from 'zod'; import { InstagramClient } from '../instagram/client.js'; import { formatErrorForMcp } from '../utils/errors.js'; export const recentPostsSchema = { username: z .string() .optional() .describe('Instagram username. Leave empty to get your own posts.'), limit: z .number() .min(1) .max(50) .default(10) .describe('Number of posts to retrieve (1-50). Default is 10.'), }; export const recentPostsDescription = 'Get recent Instagram posts with engagement metrics (likes, comments, views).'; export async function getRecentPosts( client: InstagramClient, params: { username?: string; limit?: number } ): Promise<{ content: Array<{ type: 'text'; text: string }>; isError?: boolean }> { try { const limit = params.limit ?? 10; const posts = await client.getRecentPosts(params.username, limit); const result = { count: posts.length, posts: posts.map((post) => ({ id: post.id, shortcode: post.shortcode, url: `https://www.instagram.com/p/${post.shortcode}/`, type: post.type, caption: post.caption.length > 200 ? post.caption.substring(0, 200) + '...' : post.caption, engagement: { likes: post.likeCount, comments: post.commentCount, views: post.viewCount ?? null, }, timestamp: post.timestamp, mediaUrl: post.mediaUrl, })), summary: { totalLikes: posts.reduce((sum, p) => sum + p.likeCount, 0), totalComments: posts.reduce((sum, p) => sum + p.commentCount, 0), averageLikes: posts.length > 0 ? Math.round(posts.reduce((sum, p) => sum + p.likeCount, 0) / posts.length) : 0, averageComments: posts.length > 0 ? Math.round(posts.reduce((sum, p) => sum + p.commentCount, 0) / posts.length) : 0, }, }; return { content: [ { type: 'text', text: JSON.stringify(result, null, 2), }, ], }; } catch (error) { return { isError: true, content: [ { type: 'text', text: formatErrorForMcp(error), }, ], }; } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/arjun1194/insta-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server