Skip to content

feat(mcp): read HONCHO_API_URL env var to support self-hosted Honcho instances#575

Merged
VVoruganti merged 1 commit intoplastic-labs:mainfrom
colangelo:feature/env-var-api-url
Apr 20, 2026
Merged

feat(mcp): read HONCHO_API_URL env var to support self-hosted Honcho instances#575
VVoruganti merged 1 commit intoplastic-labs:mainfrom
colangelo:feature/env-var-api-url

Conversation

@colangelo
Copy link
Copy Markdown
Contributor

@colangelo colangelo commented Apr 18, 2026

Summary

  • Read the Honcho API URL from the Worker's HONCHO_API_URL env binding,
    falling back to https://api.honcho.dev when unset.
  • Document the self-hosted workflow (.dev.vars locally, wrangler secret
    for deployed Workers) in mcp/README.md.
  • Add mcp/.dev.vars.example so the convention is discoverable.

Motivation

Closes #508. Prior PRs #540 and #503 tried to solve the same problem with an
X-Honcho-Base-URL request header; both were declined because exposing the
upstream URL as a header is a latency/security regression for the hosted
Worker. This PR uses the env-var mechanism suggested by @ajspig in #540:

The MCP server is a thin stateless proxy, so if you're self-hosting
Honcho, the natural path is to run the MCP server locally alongside it
and point it at your instance via environment variables (e.g.
HONCHO_API_URL).

Changes

File Change
mcp/src/config.ts Accept Env binding; read HONCHO_API_URL with fallback
mcp/src/index.ts Pass env to parseConfig
mcp/.dev.vars.example Document the local-dev convention
mcp/README.md Add "Self-Hosted Honcho" section

Backwards compatibility

No breaking change. HONCHO_API_URL is optional. Unset → identical behavior
to main today.

Test plan

  • bun run tsc --noEmit passes
  • Local handshake (JSON-RPC initialize) against a self-hosted Honcho at
    http://127.0.0.1:28000 succeeds with HONCHO_API_URL set
  • Same handshake against api.honcho.dev succeeds with HONCHO_API_URL unset (no regression)

Summary by CodeRabbit

  • New Features

    • Added support for configuring a custom Honcho API endpoint via the HONCHO_API_URL environment variable, allowing the Worker to integrate with self-hosted Honcho instances while maintaining backward compatibility with the default endpoint.
  • Documentation

    • Added setup instructions for deploying the MCP Worker with a self-hosted Honcho instance, including guidance for local development and production environments.

The MCP Worker hardcoded https://api.honcho.dev for every request, forcing
anyone running a self-hosted Honcho instance to patch the source before
deploying their own Worker alongside it.

Route the baseUrl through the Worker env so operators can set
HONCHO_API_URL (via .dev.vars for local development or wrangler secret for
deployed Workers) and point the Worker at their instance. The variable is
intentionally not exposed as a request header: that would let public
clients steer traffic to internal URLs, which is a latency and security
regression.

When HONCHO_API_URL is unset, the Worker falls back to
https://api.honcho.dev, so existing deployments are unaffected.

Closes plastic-labs#508
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 18, 2026

Walkthrough

The PR adds support for self-hosted Honcho instances by enabling configuration of a custom API base URL. An Env interface is introduced with an optional HONCHO_API_URL field, the parseConfig function is updated to accept and prioritize this environment variable, and the Worker entry point passes the environment context to the config parser. Documentation and example files guide local development setup.

Changes

Cohort / File(s) Summary
Configuration & Core Logic
mcp/src/config.ts, mcp/src/index.ts
Added Env interface with optional HONCHO_API_URL field; extended parseConfig to accept env parameter and prefer HONCHO_API_URL for baseUrl resolution with fallback to default; updated Worker fetch handler to type env as Env and pass it to config parser.
Documentation
mcp/README.md
Added "Self-Hosted Honcho" section documenting configuration via HONCHO_API_URL for local development (.dev.vars) and deployed environments (Wrangler secret), with fallback to https://api.honcho.dev when unset.
Configuration & Local Development
mcp/.dev.vars.example, mcp/.gitignore
Created example environment file with commented setup instructions for self-hosted endpoint; updated .gitignore to exclude .dev.vars file.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested reviewers

  • VVoruganti
  • vintrocode

Poem

🐰 With whiskers twitching, I propose with glee,
Self-hosted Honcho now comes free!
Environment variables bloom so bright,
Custom URLs in morning light! 🌟
Config flows through Worker's flight.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely summarizes the main change: reading the HONCHO_API_URL environment variable to enable self-hosted Honcho instance support.
Linked Issues check ✅ Passed The PR successfully implements the feature from issue #508 by making the Honcho API base URL configurable via environment variables instead of hardcoded, with proper fallback to the default URL.
Out of Scope Changes check ✅ Passed All changes are directly scoped to supporting self-hosted Honcho instances: environment variable handling, configuration updates, documentation, and gitignore entries.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
mcp/src/config.ts (1)

11-53: LGTM overall; consider lightweight URL validation.

The Env interface, default env = {} parameter, and env.HONCHO_API_URL?.trim() || "https://api.honcho.dev" resolution are correct and backward-compatible — empty/whitespace values correctly fall back to the managed default.

Optional hardening: if HONCHO_API_URL is set to a malformed value (e.g., missing scheme), the error surfaces later from the SDK at request time with a less obvious message. A one-line new URL(trimmed) check in parseConfig would fail fast at Worker startup/first request with a clear operator-facing error. Non-blocking.

🔧 Optional hardening
-    baseUrl: env.HONCHO_API_URL?.trim() || "https://api.honcho.dev",
+    baseUrl: (() => {
+      const override = env.HONCHO_API_URL?.trim();
+      if (!override) return "https://api.honcho.dev";
+      try {
+        new URL(override);
+      } catch {
+        throw new Error(
+          `Invalid HONCHO_API_URL: ${override}. Must be an absolute URL (e.g., http://127.0.0.1:8000).`,
+        );
+      }
+      return override;
+    })(),

Note: parseConfig errors currently return 401 in index.ts; a misconfigured env var is really a 500-class condition, so if you adopt this you may also want to distinguish config errors from auth errors in the caller.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@mcp/src/config.ts` around lines 11 - 53, parseConfig currently accepts
HONCHO_API_URL values as-is which can be malformed and cause confusing errors
later; add a lightweight URL validation by trimming env.HONCHO_API_URL and, when
non-empty, passing it to new URL(...) to throw early on invalid values (update
the Env usage in parseConfig and the baseUrl resolution logic that computes
baseUrl to validate the trimmedHONCHO variable before falling back to
"https://api.honcho.dev"). Ensure you reference Env, parseConfig,
HONCHO_API_URL, and baseUrl so the change is easy to locate.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@mcp/src/config.ts`:
- Around line 11-53: parseConfig currently accepts HONCHO_API_URL values as-is
which can be malformed and cause confusing errors later; add a lightweight URL
validation by trimming env.HONCHO_API_URL and, when non-empty, passing it to new
URL(...) to throw early on invalid values (update the Env usage in parseConfig
and the baseUrl resolution logic that computes baseUrl to validate the
trimmedHONCHO variable before falling back to "https://api.honcho.dev"). Ensure
you reference Env, parseConfig, HONCHO_API_URL, and baseUrl so the change is
easy to locate.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4dbc07f0-5e7a-4169-8d78-7df46782a071

📥 Commits

Reviewing files that changed from the base of the PR and between 9676526 and 3657c07.

📒 Files selected for processing (5)
  • mcp/.dev.vars.example
  • mcp/.gitignore
  • mcp/README.md
  • mcp/src/config.ts
  • mcp/src/index.ts

@VVoruganti VVoruganti merged commit 51497f1 into plastic-labs:main Apr 20, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] MCP Worker: support custom base URL for self-hosted Honcho instances

2 participants