Skip to main content
Glama

Reddit MCP Server

by ozipi
get-post.ts2.17 kB
import { getPostSuccessMessage } from '../../constants/tool/get-post.js'; import { RedditError } from '../../types/reddit.js'; import { formatToolResponse } from './types.js'; import type { ToolHandler, GetPostArgs} from './types.js'; export const handleGetPost: ToolHandler<GetPostArgs> = async (args, { redditService }) => { try { const { id } = args; if (!id) { throw new RedditError("id is required for fetching posts", "VALIDATION_ERROR"); } const postWithComments = await redditService.fetchPostById(id); const formattedPost = { ...postWithComments, comments: formatCommentsForDisplay(postWithComments.comments), }; return formatToolResponse({ message: getPostSuccessMessage, result: { post: formattedPost, }, }); } catch (error) { return formatToolResponse({ status: "error", message: `Failed to fetch Reddit post: ${error instanceof Error ? error.message : "Unknown error"}`, error: { type: error instanceof RedditError ? error.type : "API_ERROR", details: error, }, }); } }; /** * Formats comment threads for better display in the tool response * @param comments The comment threads to format * @param depth The current depth level (for indentation) * @returns Formatted comments */ function formatCommentsForDisplay(comments: any[], depth = 0): any[] { if (!comments || !Array.isArray(comments) || comments.length === 0) { return []; } return comments.map((thread) => { const { comment, replies } = thread; // Add depth and format the comment const formattedComment = { ...comment, depth, // Truncate very long comments for display body: comment.body.length > 1000 ? `${comment.body.substring(0, 1000)}... (truncated)` : comment.body, formattedTime: new Date(comment.createdUtc * 1000).toISOString(), }; // Format replies recursively with increased depth const formattedReplies = formatCommentsForDisplay(replies, depth + 1); return { comment: formattedComment, replies: formattedReplies, }; }); }

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/ozipi/brainloop-mcp-server-v2'

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