claude-agent-server 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '09c3725ebdfb2ef25ab33f08e3d76e60b9805ec5b466aa2191e92816a948cbe5'
4
+ data.tar.gz: f39f47d131e6654de322d62221692cf6ec433f4f65fa69b253529a6fbe7402cd
5
+ SHA512:
6
+ metadata.gz: 2a768a352fa1ec65643997d1cc1ad06dd9446726b9c85e920678ad49eb156c2e3abef5ab0ebefe566610a607fe6704e72d34941af5fe094cc89009d3a711e9d3
7
+ data.tar.gz: 86103d694771510121f761f65f80eee7ea56dae9a18dcee0a62f8d3de905df1de7ef40741e4fbf026b461762e049a8842d3897c4ad63702ce0e6752ac5432d01
data/CHANGELOG.md ADDED
@@ -0,0 +1,24 @@
1
+ # Changelog
2
+
3
+ ## [0.1.0] - 2026-03-05
4
+
5
+ ### Added
6
+ - Initial release
7
+ - REST API for one-shot queries (`POST /query`, `POST /query/stream`)
8
+ - Interactive session management (`POST /sessions`, `DELETE /sessions/:id`)
9
+ - Session messaging (`POST /sessions/:id/messages`)
10
+ - SSE streaming for session messages (`GET /sessions/:id/messages/stream`)
11
+ - Session interrupt (`POST /sessions/:id/interrupt`)
12
+ - Model switching (`POST /sessions/:id/model`)
13
+ - MCP status (`GET /sessions/:id/mcp-status`)
14
+ - Session history (`GET /sessions/:id/history`)
15
+ - CLI session browsing (`GET /cli-sessions`, `GET /cli-sessions/:id/messages`)
16
+ - Health check (`GET /health`) and server info (`GET /info`)
17
+ - Bearer token authentication with timing-safe comparison
18
+ - CORS support with origin validation
19
+ - Request ID propagation
20
+ - Error handling middleware (SDK errors mapped to HTTP status codes)
21
+ - CLI executable (`claude-agent-server`) with Falcon
22
+ - Docker support
23
+ - Session TTL with automatic reaper
24
+ - Configurable session limits
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Community Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,213 @@
1
+ # claude-agent-server
2
+
3
+ HTTP server wrapping the [Claude Agent Ruby SDK](https://github.com/ya-luotao/claude-agent-sdk-ruby) as a REST + SSE API. Expose Claude Code as a network service with session management, streaming, and authentication.
4
+
5
+ ## Requirements
6
+
7
+ - Ruby 3.2+
8
+ - Claude Code CLI 2.0.0+ (`npm install -g @anthropic-ai/claude-code`)
9
+ - `ANTHROPIC_API_KEY` environment variable
10
+
11
+ ## Installation
12
+
13
+ ```bash
14
+ gem install claude-agent-server
15
+ ```
16
+
17
+ Or add to your Gemfile:
18
+
19
+ ```ruby
20
+ gem 'claude-agent-server'
21
+ ```
22
+
23
+ ## Quick Start
24
+
25
+ ```bash
26
+ # Start the server
27
+ claude-agent-server --port 9292
28
+
29
+ # One-shot query
30
+ curl -X POST http://localhost:9292/query \
31
+ -H 'Content-Type: application/json' \
32
+ -d '{"prompt":"Say hello"}'
33
+
34
+ # Streaming query (SSE)
35
+ curl -N -X POST http://localhost:9292/query/stream \
36
+ -H 'Content-Type: application/json' \
37
+ -d '{"prompt":"Say hello"}'
38
+
39
+ # Interactive session
40
+ curl -X POST http://localhost:9292/sessions \
41
+ -H 'Content-Type: application/json' \
42
+ -d '{"prompt":"Hello"}'
43
+
44
+ # Send message to session
45
+ curl -X POST http://localhost:9292/sessions/<id>/messages \
46
+ -H 'Content-Type: application/json' \
47
+ -d '{"prompt":"What is 2+2?"}'
48
+
49
+ # Stream session messages (SSE)
50
+ curl -N http://localhost:9292/sessions/<id>/messages/stream
51
+ ```
52
+
53
+ ## Configuration
54
+
55
+ ### Environment Variables
56
+
57
+ | Variable | Default | Description |
58
+ |----------|---------|-------------|
59
+ | `CLAUDE_SERVER_HOST` | `0.0.0.0` | Bind address |
60
+ | `CLAUDE_SERVER_PORT` | `9292` | Listen port |
61
+ | `CLAUDE_SERVER_AUTH_TOKEN` | (none) | Bearer token for authentication |
62
+ | `CLAUDE_SERVER_CORS_ORIGINS` | `*` | Comma-separated allowed origins |
63
+ | `CLAUDE_SERVER_SESSION_TTL` | `3600` | Session idle timeout (seconds) |
64
+ | `CLAUDE_SERVER_MAX_SESSIONS` | `100` | Maximum concurrent sessions |
65
+ | `CLAUDE_SERVER_LOG_LEVEL` | `info` | Log level |
66
+
67
+ ### CLI Options
68
+
69
+ ```
70
+ Usage: claude-agent-server [options]
71
+ -p, --port PORT Port to listen on (default: 9292)
72
+ -b, --bind HOST Host to bind to (default: 0.0.0.0)
73
+ -t, --token TOKEN Authentication token
74
+ --session-ttl SECONDS Session TTL in seconds (default: 3600)
75
+ --max-sessions N Maximum concurrent sessions (default: 100)
76
+ --cors-origins ORIGINS Comma-separated CORS origins
77
+ -v, --version Show version
78
+ -h, --help Show help
79
+ ```
80
+
81
+ ### Ruby Configuration
82
+
83
+ ```ruby
84
+ require 'claude_agent_server'
85
+
86
+ ClaudeAgentServer.configure do |config|
87
+ config.auth_token = 'my-secret-token'
88
+ config.max_sessions = 50
89
+ config.default_sdk_options = { model: 'claude-sonnet-4-20250514' }
90
+ end
91
+
92
+ run ClaudeAgentServer.app
93
+ ```
94
+
95
+ ## API Reference
96
+
97
+ ### Health
98
+
99
+ | Method | Path | Auth | Description |
100
+ |--------|------|------|-------------|
101
+ | GET | `/health` | No | Health check |
102
+ | GET | `/info` | Yes | Server version, SDK version, active sessions |
103
+
104
+ ### One-Shot Query
105
+
106
+ | Method | Path | Auth | Description |
107
+ |--------|------|------|-------------|
108
+ | POST | `/query` | Yes | Execute query, return JSON array of messages |
109
+ | POST | `/query/stream` | Yes | Execute query, return SSE stream |
110
+
111
+ **Request body:**
112
+ ```json
113
+ {
114
+ "prompt": "Your prompt here",
115
+ "options": {
116
+ "model": "claude-sonnet-4-20250514",
117
+ "maxTurns": 5,
118
+ "allowedTools": ["Read", "Bash"]
119
+ }
120
+ }
121
+ ```
122
+
123
+ ### Interactive Sessions
124
+
125
+ | Method | Path | Auth | Description |
126
+ |--------|------|------|-------------|
127
+ | GET | `/sessions` | Yes | List active sessions |
128
+ | POST | `/sessions` | Yes | Create session (optionally with initial prompt) |
129
+ | GET | `/sessions/:id` | Yes | Get session info |
130
+ | DELETE | `/sessions/:id` | Yes | Disconnect and cleanup |
131
+ | POST | `/sessions/:id/messages` | Yes | Send message to session |
132
+ | GET | `/sessions/:id/messages/stream` | Yes | SSE stream of session messages |
133
+ | POST | `/sessions/:id/interrupt` | Yes | Interrupt current turn |
134
+ | POST | `/sessions/:id/model` | Yes | Switch model mid-session |
135
+ | GET | `/sessions/:id/mcp-status` | Yes | Get MCP server status |
136
+ | GET | `/sessions/:id/history` | Yes | Get session transcript |
137
+
138
+ ### CLI Session Browsing
139
+
140
+ | Method | Path | Auth | Description |
141
+ |--------|------|------|-------------|
142
+ | GET | `/cli-sessions` | Yes | List past CLI sessions (read-only) |
143
+ | GET | `/cli-sessions/:id/messages` | Yes | Get session transcript |
144
+
145
+ ## SSE Event Format
146
+
147
+ ```
148
+ event: assistant
149
+ data: {"type":"assistant","content":[{"type":"text","text":"Hello!"}],"model":"claude-sonnet-4-20250514"}
150
+
151
+ event: result
152
+ data: {"type":"result","subtype":"result","durationMs":1200,"isError":false}
153
+
154
+ event: done
155
+ data: {"status":"complete"}
156
+ ```
157
+
158
+ ## Authentication
159
+
160
+ When `CLAUDE_SERVER_AUTH_TOKEN` is set, all routes except `/health` require a Bearer token:
161
+
162
+ ```bash
163
+ curl -H 'Authorization: Bearer your-token' http://localhost:9292/info
164
+ ```
165
+
166
+ Uses timing-safe comparison to prevent timing attacks.
167
+
168
+ ## Error Responses
169
+
170
+ All errors return JSON:
171
+
172
+ ```json
173
+ {
174
+ "error": {
175
+ "code": "session_not_found",
176
+ "message": "Session 'abc-123' not found"
177
+ }
178
+ }
179
+ ```
180
+
181
+ | HTTP Status | Code | Description |
182
+ |-------------|------|-------------|
183
+ | 400 | `bad_request` | Invalid input |
184
+ | 401 | `unauthorized` | Missing or invalid auth token |
185
+ | 404 | `session_not_found` | Session does not exist |
186
+ | 429 | `session_limit_reached` | Max sessions exceeded |
187
+ | 502 | `cli_connection_error` | Claude CLI failed |
188
+ | 503 | `cli_not_found` | Claude CLI not installed |
189
+ | 500 | `internal_error` | Unexpected error |
190
+
191
+ ## Docker
192
+
193
+ ```bash
194
+ docker build -t claude-agent-server .
195
+ docker run -p 9292:9292 \
196
+ -e ANTHROPIC_API_KEY=your-key \
197
+ -e CLAUDE_SERVER_AUTH_TOKEN=your-token \
198
+ claude-agent-server
199
+ ```
200
+
201
+ ## Development
202
+
203
+ ```bash
204
+ bundle install
205
+ bundle exec rspec # Run unit tests
206
+ bundle exec rubocop # Run linter
207
+ bundle exec rake # Run both
208
+ RUN_INTEGRATION=1 bundle exec rspec # Include integration tests
209
+ ```
210
+
211
+ ## License
212
+
213
+ MIT
data/config.ru ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/claude_agent_server'
4
+
5
+ # Configure from environment variables (already defaults in Config)
6
+ ClaudeAgentServer.configure do |config|
7
+ # Override defaults here if needed
8
+ end
9
+
10
+ run ClaudeAgentServer.app