Pexels MCP Server

Pexels MCP Server

Enables searching and displaying high-quality, royalty-free photos from Pexels directly in ChatGPT conversations via an interactive gallery widget.

Category
Visit Server

README

Pexels ChatGPT App

A ChatGPT App that brings high-quality, royalty-free photography from Pexels directly into your conversations. Built with the Model Context Protocol (MCP) and powered by Cloudflare Workers.

Version TypeScript React Cloudflare Workers


Pexels ChatGPT App Demo


What is This?

This project is a Model Context Protocol (MCP) server that integrates the Pexels API into ChatGPT. Users can search for professional stock photos directly in their chat conversations and view results in an interactive, responsive gallery widget.

Key Features

  • šŸ” Advanced Photo Search - Search by keywords, orientation, color palette, size, and more
  • šŸŽØ Interactive Gallery Widget - Beautiful carousel interface with photo cards
  • šŸŒ“ Theme Aware - Automatically adapts to light/dark mode
  • ♿ Accessible - Full screen reader support and keyboard navigation
  • ⚔ Serverless - Runs on Cloudflare Workers edge network for global performance
  • šŸ“± Responsive - Works seamlessly across desktop, tablet, and mobile

Architecture

This project demonstrates a complete MCP application architecture:

ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
│                         ChatGPT UI                          │
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
│  React Widget (Embedded Gallery)                            │
│  ↓ Uses OpenAI SDK                                          │
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
│  MCP Protocol (SSE)                                          │
│  ↓ Tool Invocation                                          │
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
│  Cloudflare Worker (Durable Objects)                        │
│  ↓ HTTP Request                                             │
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
│  Pexels API (https://api.pexels.com)                        │
ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜

Tech Stack

Backend:

Frontend:

External API:


Project Structure

pexels-app/
ā”œā”€ā”€ src/                              # Backend (Cloudflare Worker)
│   ā”œā”€ā”€ index.ts                      # MCP Durable Object & worker entry
│   ā”œā”€ā”€ types.ts                      # Shared TypeScript types
│   ā”œā”€ā”€ tools/
│   │   └── pexels.ts                 # Pexels search tool implementation
│   └── components/
│       ā”œā”€ā”€ react-widget-inline.ts    # Bundled React widget (auto-generated)
│       └── react-widget-resource.ts  # HTML wrapper for widget
│
ā”œā”€ā”€ web/                              # Frontend (React Widget)
│   ā”œā”€ā”€ src/
│   │   ā”œā”€ā”€ component.tsx             # React entry point
│   │   ā”œā”€ā”€ theme.tsx                 # Theme tokens & context
│   │   ā”œā”€ā”€ components/
│   │   │   ā”œā”€ā”€ App.tsx               # Main gallery application
│   │   │   └── cards/                # Photo card components
│   │   └── hooks/
│   │       ā”œā”€ā”€ use-openai-global.ts  # OpenAI SDK integration
│   │       └── use-widget-state.ts   # Persistent state management
│   └── dist/                         # Build output
│
ā”œā”€ā”€ scripts/
│   └── inline-react-widget.js        # Bundles React into Worker
│
ā”œā”€ā”€ docs/                             # Comprehensive documentation
│   ā”œā”€ā”€ ARCHITECTURE.md               # System design
│   ā”œā”€ā”€ DEPLOYMENT-GUIDE.md           # Deployment instructions
│   └── ...                           # More guides
│
ā”œā”€ā”€ wrangler.jsonc                    # Cloudflare Worker config
ā”œā”€ā”€ .dev.vars.example                 # Environment variable template
└── package.json                      # Dependencies & scripts

Getting Started

Prerequisites

Installation

  1. Clone the repository:

    git clone https://github.com/yourusername/pexels-app.git
    cd pexels-app
    
  2. Install dependencies:

    npm install
    cd web && npm install && cd ..
    
  3. Configure environment variables:

    cp .dev.vars.example .dev.vars
    

    Edit .dev.vars and add your Pexels API key:

    PEXELS_API_KEY=your_actual_api_key_here
    
  4. Build and run locally:

    npm run dev
    

    The MCP server will be available at http://localhost:8787

Deployment

  1. Authenticate with Cloudflare:

    npx wrangler login
    
  2. Set production secrets:

    npx wrangler secret put PEXELS_API_KEY
    # Enter your API key when prompted
    
  3. Deploy to Cloudflare:

    npm run deploy
    

    Your app will be live at https://your-worker-name.workers.dev


Usage

MCP Tool: pexels.searchPhotos

Search the Pexels library with powerful filtering options:

Parameters:

Parameter Type Required Description
query string Yes Search keywords (1-120 characters)
page number No Page number (1-50, default: 1)
perPage number No Results per page (1-30, default: 12)
orientation string No landscape, portrait, or square
size string No large, medium, or small
color string No Hex code or color keyword
locale string No ISO locale code (e.g., en-US)

Example in ChatGPT:

User: Show me portrait photos of mountain landscapes

[ChatGPT invokes: pexels.searchPhotos({
  query: "mountain landscapes",
  orientation: "portrait",
  perPage: 15
})]

[Interactive gallery widget displays with 15 photos]

Output:

  • Text summary: "Found X Pexels photos for 'query'"
  • Interactive gallery widget with photo cards
  • Each card shows:
    • High-quality photo preview
    • Photographer attribution
    • "View on Pexels" button (opens in new tab)

Development

NPM Scripts

# Development
npm start                 # Start local dev server
npm run dev              # Same as start

# Building
npm run build:widget     # Build React widget only
npm run deploy           # Build everything and deploy

# React Development (in web/ directory)
cd web
npm run build            # Build React bundle
npm run watch            # Watch mode for React changes

# Type Checking
npm run type-check       # Check TypeScript types

Development Workflow

  1. Make changes to React widget (web/src/)
  2. Build widget: cd web && npm run build
  3. Inline bundle: node scripts/inline-react-widget.js
  4. Test locally: npm run dev
  5. Deploy: npm run deploy

For faster iteration, use watch mode in a separate terminal:

cd web && npm run watch

Configuration

Environment Variables

Development (.dev.vars):

PEXELS_API_KEY=your_api_key_here

Production (Cloudflare Secrets):

npx wrangler secret put PEXELS_API_KEY
npx wrangler secret list  # View configured secrets

Wrangler Configuration

Key settings in wrangler.jsonc:

{
  "name": "pexels",
  "main": "src/index.ts",
  "compatibility_date": "2025-03-10",
  "durable_objects": {
    "bindings": [{
      "class_name": "MyMCP",
      "name": "MCP_OBJECT"
    }]
  },
  "vars": {
    "PEXELS_API_BASE_URL": "https://api.pexels.com/v1"
  }
}

Documentation

Comprehensive documentation is available in the docs/ directory:


Design Philosophy

This project follows OpenAI's ChatGPT App design guidelines:

  1. Conversational - Seamlessly integrated into chat flow
  2. Intelligent - Context-aware tool invocation
  3. Simple - Focused, single-purpose interactions
  4. Responsive - Fast, lightweight, edge-optimized
  5. Accessible - Screen reader support, keyboard navigation

UI/UX Principles

  • System-First Design - Inherits ChatGPT's colors, fonts, and spacing
  • Theme Tokens - Dynamic light/dark theme support
  • Transparent Backgrounds - Blends naturally with host environment
  • Component-Scoped Styles - No global CSS to avoid conflicts
  • Minimal Branding - Only subtle accent colors on CTAs

API Reference

Pexels API Integration

The tool makes requests to the Pexels API v1:

Endpoint: GET https://api.pexels.com/v1/search

Authentication: Bearer token via Authorization header

Rate Limits: 200 requests/hour (free tier)

Response Format: Normalized from snake_case to camelCase for TypeScript

For full Pexels API documentation, visit: https://www.pexels.com/api/documentation/


Advanced Features

Custom React Hooks

useOpenAiGlobal(key) - Access OpenAI SDK globals

const theme = useOpenAiGlobal('theme'); // 'light' | 'dark'
const toolOutput = useOpenAiGlobal('toolOutput'); // Tool result data

useWidgetState(defaultState) - Persistent widget state

const [state, setState] = useWidgetState({ page: 1 });
// State survives widget remounts

useThemeTokens() - Access theme design tokens

const tokens = useThemeTokens();
// tokens.colors.background, tokens.fonts.body, etc.

Type Safety

  • Full TypeScript coverage with strict mode
  • Zod validation for all tool inputs
  • Shared types between frontend and backend
  • Automatic type inference from OpenAI SDK

Error Handling

Graceful handling of:

  • API authentication failures
  • Network errors
  • Missing configuration
  • Invalid parameters
  • Empty search results
  • User-friendly error messages in widget

Security

Content Security Policy

The widget enforces a strict CSP:

default-src 'none';
script-src 'self' 'unsafe-inline';
style-src 'self' 'unsafe-inline';
img-src 'self' data: https://images.pexels.com;
frame-ancestors 'none';

Best Practices

  • API keys stored as Cloudflare secrets (never in code)
  • .dev.vars.example provided as template
  • Input validation with Zod schemas

License

This project is licensed under the MIT License - see the LICENSE file for details.


Acknowledgments


Learn More

MCP Resources

Cloudflare Resources

ChatGPT App Development


Built with ā¤ļø using the Model Context Protocol

Recommended Servers

playwright-mcp

playwright-mcp

A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.

Official
Featured
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.

Official
Featured
Local
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.

Official
Featured
Local
TypeScript
VeyraX MCP

VeyraX MCP

Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.

Official
Featured
Local
graphlit-mcp-server

graphlit-mcp-server

The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.

Official
Featured
TypeScript
Kagi MCP Server

Kagi MCP Server

An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.

Official
Featured
Python
E2B

E2B

Using MCP to run code via e2b.

Official
Featured
Neon Database

Neon Database

MCP server for interacting with Neon Management API and databases

Official
Featured
Exa Search

Exa Search

A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.

Official
Featured
Qdrant Server

Qdrant Server

This repository is an example of how to create a MCP server for Qdrant, a vector search engine.

Official
Featured