instagram-server-next-mcp
by: duhlink
instagram server next mcp
πOverview
Purpose: To provide a Model Context Protocol (MCP) server for fetching Instagram posts utilizing Chrome's existing login session.
Overview: The Instagram MCP Server is designed to efficiently retrieve Instagram posts by leveraging a modular architecture and TypeScript for type safety. It supports JSON-RPC 2.0 for seamless communication and includes robust error handling.
Key Features:
-
Modular Architecture: Promotes clear separation of concerns, enhancing maintainability and extensibility of the code.
-
Type Safety with TypeScript: Provides comprehensive type definitions for improved error catching and better integration with development tools.
-
Error Handling: Implements standardized error codes and informative messages to facilitate troubleshooting.
-
Configurable via Environment Variables: Allows users to adjust settings for Chrome user data and other parameters easily.
-
JSON-RPC 2.0 Compliance: Supports efficient and standardized communication protocols.
-
Automatic Media Handling: Includes features for downloading media and generating associated metadata, along with an SEO-friendly description generation.
Instagram MCP Server
A Model Context Protocol (MCP) server for fetching Instagram posts using Chrome's existing login session.
Features
- Modular architecture with clear separation of concerns
- Type-safe implementation using TypeScript
- Improved error handling and logging
- Configurable through environment variables
- JSON-RPC 2.0 compliant communication
- Automatic media downloading and metadata generation
- SEO-friendly description generation
Architecture
The server follows a modular architecture with the following structure:
src/
βββ core/ # Core MCP functionality
β βββ mcp/ # MCP server implementation
β β βββ server.ts # Server class
β β βββ stdio.ts # StdioServerTransport
β β βββ index.ts # Barrel exports
β βββ types/ # Core type definitions
β β βββ mcp.ts # MCP types
β βββ utils/ # Utility functions
β βββ config.ts # Configuration management
β βββ errors.ts # Error handling
βββ features/ # Feature modules
β βββ instagram/ # Instagram feature
β βββ types.ts # Instagram types
β βββ utils/ # Feature utilities
β β βββ media.ts # Media handling
β β βββ post.ts # Post processing
β β βββ seo.ts # SEO generation
β βββ instagram.service.ts # Instagram service
βββ services/ # Shared services
β βββ browser/ # Browser service
β βββ types.ts # Browser types
β βββ browser.service.ts # Browser service
βββ index.ts # Entry point
βββ server.ts # Main server class
Configuration
The server requires the following environment variables:
CHROME_USER_DATA_DIR
: Path to Chrome user data directory containing login session
Additional configuration options are available through the config manager:
- Browser settings (viewport, timeouts)
- Instagram settings (delays, batch sizes)
- Save directory and file paths
Usage
-
Install dependencies:
npm install
-
Build the server:
npm run build
-
Run the server:
CHROME_USER_DATA_DIR=/path/to/chrome/profile npm start
Available Tools
get_instagram_posts
Fetches recent posts from an Instagram profile.
Parameters:
username
(required): Instagram username to fetch posts fromlimit
(optional): Number of posts to fetch (1-50) or "all"saveDir
(optional): Directory to save media files and metadatadelayBetweenPosts
(optional): Milliseconds to wait between processing posts
Example request:
{
"jsonrpc": "2.0",
"id": 1,
"method": "call_tool",
"params": {
"name": "get_instagram_posts",
"arguments": {
"username": "example",
"limit": 10
}
}
}
Error Handling
The server uses standardized error codes and messages:
INVALID_REQUEST
: Invalid request format or parametersINVALID_PARAMS
: Missing or invalid parametersMETHOD_NOT_FOUND
: Unknown method or toolINTERNAL_ERROR
: Server-side errors
Development
-
Start in development mode:
npm run dev
-
Run linter:
npm run lint
Improvements Over Original
-
Modular Architecture
- Clear separation of concerns
- Better code organization
- Easier to maintain and extend
-
Type Safety
- Comprehensive TypeScript types
- Better error catching
- Improved IDE support
-
Error Handling
- Standardized error codes
- Better error messages
- Proper error propagation
-
Configuration
- Centralized configuration
- Environment variable validation
- Type-safe config access
-
Code Quality
- Consistent coding style
- Better documentation
- Improved logging
-
Testing Support
- Modular design enables testing
- Dependency injection ready
- Clear interfaces
License
MIT