feat: add Anthropic Messages API endpoint (/v1/messages)#28
Open
suyuan2022 wants to merge 4 commits intoatalovesyou:mainfrom
Open
feat: add Anthropic Messages API endpoint (/v1/messages)#28suyuan2022 wants to merge 4 commits intoatalovesyou:mainfrom
suyuan2022 wants to merge 4 commits intoatalovesyou:mainfrom
Conversation
Add native Anthropic Messages API support alongside the existing OpenAI-compatible endpoint. This enables direct integration with tools that use the Anthropic format (e.g., OpenClaw/Clawdbot), preserving tool_use blocks, usage stats, and streaming events. New files: - src/types/anthropic.ts: Anthropic Messages API type definitions - src/adapter/anthropic-to-cli.ts: request conversion (Anthropic → CLI) - src/adapter/cli-to-anthropic.ts: response conversion (CLI → Anthropic) Modified: - src/server/routes.ts: add handleAnthropicMessages handler - src/server/index.ts: register /v1/messages route Both streaming (SSE with event types) and non-streaming modes are supported and tested. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Author
Production Verification ✅This has been deployed and tested in a real production environment: Setup:
Results:
Note: When configuring 🤖 Generated with Claude Code |
Without this flag, CLI tools like WebSearch, WebFetch, Read etc. are blocked in --print mode because there's no interactive user to approve permissions. This caused "permission denied" errors when used through OpenClaw. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…andling - Save base64 images to temp files for CLI Read tool access - Handle tool_use and tool_result content blocks in prompt serialization - Clean up temp files after subprocess completes - Add AnthropicImageContent/AnthropicImageSource types Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…-skip-permissions --dangerously-skip-permissions cannot be used under root/sudo. The proxy runs as root via systemd, so switch to --permission-mode bypassPermissions. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
/v1/messages) endpoint alongside the existing OpenAI-compatible/v1/chat/completionsevent:types) and non-streaming modes supportedMotivation
Many tools (OpenClaw/Clawdbot, etc.) work best with Anthropic's native Messages API format. The current OpenAI-only format causes issues:
tool_useblocks get lost in OpenAI format conversion (related to Response format incompatible with Clawdbot's OpenAI provider #2)cache_creation_input_tokens/cache_read_input_tokensare droppedThis PR adds a parallel Anthropic endpoint so clients can choose their preferred format.
Changes
src/types/anthropic.tssrc/adapter/anthropic-to-cli.tssrc/adapter/cli-to-anthropic.tssrc/server/routes.tshandleAnthropicMessageshandlersrc/server/index.ts/v1/messagesrouteStreaming format
Follows Anthropic's SSE spec exactly:
Test plan
/v1/messagesreturns proper 400 for empty messages/v1/messagesreturns proper 400 for missingmax_tokens/v1/chat/completionsendpoint unaffected