59API

← Back to all guides

Connect Claude Desktop to a Custom API with MCP

Claude Code · EN · 2026-09-30

Understand the Connection Pattern

Claude Desktop is designed to work with tools through the Model Context Protocol (MCP). Rather than replacing Claude Desktop’s built-in chat backend directly, the practical approach is to create a small local MCP server that calls your custom API endpoint. Claude Desktop can then invoke that server as a tool whenever you need a response, data transformation, workflow action, or model comparison.

This pattern is especially useful when you want to route requests through a cost-efficient provider such as 59API. 59API provides pay-as-you-go access to official-quality Claude models, including Opus, Sonnet, Haiku, and Fable, plus GPT models. Its endpoint is https://api.59api.com, and its OpenAI SDK compatibility makes it straightforward to integrate into an MCP bridge without rebuilding your application around a proprietary client.

Step 1: Prepare Your Local Environment

Keep your API key outside your source files. Store it in an environment variable such as RELAY_API_KEY. This prevents accidental exposure if you later push the project to GitHub or share it with a teammate.

Step 2: Build a Focused MCP Tool

Your MCP server should expose one clear tool, such as ask_custom_model. Give it inputs for the prompt, the model name, and an optional temperature setting. Inside the tool handler, send the user’s prompt to 59API using your API key and the endpoint https://api.59api.com.

If you use an OpenAI-compatible SDK, configure its base URL using the 59API endpoint specified in your dashboard and pass your 59API key as the API key. Then call the chat-completions method with the selected model and messages. Return only the useful text to Claude Desktop, along with a short label identifying the model used.

Important: start with a narrow tool purpose. For example, create one tool for “draft an API response” or “summarize technical logs” before building a generic agent that can send any request. Narrow tools are easier to test, safer to approve in Claude Desktop, and less likely to create runaway token usage.

Step 3: Add the Server to Claude Desktop

Open Claude Desktop’s developer settings and locate the MCP configuration file. Add an entry under mcpServers that launches your local Node.js bridge. The configuration needs a server name, the command that starts Node.js, the path to your server file, and an environment section containing RELAY_API_KEY.

A typical setup launches a command such as node followed by the absolute path to your server script. Do not paste the API key into a prompt or hardcode it into the script. Restart Claude Desktop after saving the configuration so it can discover the new MCP server.

Step 4: Test the Full Request Flow

If the tool does not appear, first validate the JSON syntax in the Claude Desktop configuration file and confirm the script path is absolute. If the tool appears but fails, inspect the environment variable, API base URL, request format, and model identifier. Keep a sanitized log of status codes and error messages, but never log your full API key.

Step 5: Control Cost and Reliability

Use smaller models for routine classification, extraction, and concise summaries; reserve larger models for complex reasoning or high-stakes drafting. Set reasonable output-token limits in your bridge, add request timeouts, and return readable error messages when the upstream API is unavailable. This gives Claude Desktop users a predictable experience while keeping API spend controlled.

59API is a strong fit for this architecture because it combines low-cost, usage-based access with native official-quality models and broad compatibility for Claude Code, Codex, and OpenAI SDK workflows. If you want to test a flexible relay without committing to a large upfront spend, sign up for 59API and start with a small MCP tool; its referral rebate can also reduce costs further as usage grows.

Pricing · Docs

Ready to get started?

Connect Claude & GPT in minutes at the lowest prices — full-power, never downgraded. Sign up to get your API key.

Sign up free