Sockpuppet for Claude Code — drive the TUI programmatically, stream JSONL like claude -p
Project description
claude-sock
A sockpuppet for Claude Code. Drive the TUI programmatically via a pty, stream JSONL output like claude -p.
Why
claude -p (pipe mode) is great but limited — no MCP servers, no skills, no interactive tool use. claude-sock spawns the full Claude Code TUI, injects keystrokes through a pty, and reads structured output from the session JSONL file. Your code gets the full power of interactive Claude Code through a simple async Python API.
Install
pip install claude-sock
Requires Claude Code CLI (claude) to be installed and authenticated.
Quick start
Python API
import asyncio
from claude_sock.orchestrator import ClaudeREPL
async def main():
async with ClaudeREPL(timeout=60) as repl:
messages = await repl.query("What files are in this directory?")
for msg in messages:
print(msg)
asyncio.run(main())
Streaming
async with ClaudeREPL() as repl:
async for msg in repl.query_stream("Refactor main.py"):
print(msg)
Resume a session
async with ClaudeREPL(resume="ef4f97fd-d265-474e-9544-3f228e9654c0") as repl:
messages = await repl.query("Continue where we left off")
Load skills
async with ClaudeREPL() as repl:
await repl.send_skill("commit")
messages = await repl.query("Fix the login bug and commit")
MCP servers
# Pick servers by name from your .mcp.json
async with ClaudeREPL(server_names=["my-server"]) as repl:
messages = await repl.query("Use the my-server tool")
CLI (drop-in claude -p replacement)
echo "Explain this repo" | claude-sock -p
claude-sock "What does main.py do?" --timeout 60
claude-sock "Continue" --resume ef4f97fd-d265-474e-9544-3f228e9654c0
Output is JSONL, compatible with anything that reads claude -p format.
How it works
Your code
│
▼ keystrokes
┌─────────┐ ┌──────────────┐
│ pty │───────▶│ Claude Code │
│ (write) │ │ TUI │
└─────────┘ └──────┬───────┘
│ writes
▼
┌──────────────┐
│ session.jsonl │
└──────┬───────┘
│ reads
▼
┌──────────────┐
│ Your code │
│ (parsed) │
└──────────────┘
- Write channel: pty file descriptor — fire keystrokes and forget
- Read channel:
~/.claude/projects/…/{session_id}.jsonl— poll for new lines by byte offset
API reference
ClaudeREPL(timeout, session_id, workdir, env, server_names, resume)
| Param | Type | Default | Description |
|---|---|---|---|
timeout |
float |
120 |
Seconds to wait for activity before timing out |
session_id |
str | None |
auto-generated | Force a specific session ID |
workdir |
Path | None |
cwd |
Working directory for Claude |
env |
dict | None |
None |
Extra environment variables |
server_names |
list[str] | None |
None |
MCP server names from .mcp.json |
resume |
str | None |
None |
Session ID to resume |
Methods
| Method | Returns | Description |
|---|---|---|
query(text) |
list[Message] |
Send a message, wait for completion |
query_stream(text) |
AsyncIterator[Message] |
Send a message, yield parsed messages |
query_stream_raw(text) |
AsyncIterator[dict] |
Send a message, yield raw JSONL dicts |
send_skill(name) |
list[Message] |
Load a slash command (e.g. "commit") |
Message types
AssistantMessage— containsTextBlockandToolUseBlockToolResultMessage— containsToolResultBlockResultMessage— turn summary (duration, cost, session ID)
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file claude_sock-0.1.0.tar.gz.
File metadata
- Download URL: claude_sock-0.1.0.tar.gz
- Upload date:
- Size: 9.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1d00507a24743e780778334ee82b6c48c946b1aa4d0523527e97f6e326f4c8f1
|
|
| MD5 |
213fc19d5da02d6fd0ffc378c7299e2c
|
|
| BLAKE2b-256 |
95d45df24b1d48b72a7f68d36b12a2180781f28b614f3e952056d5f06ac5ced7
|
File details
Details for the file claude_sock-0.1.0-py3-none-any.whl.
File metadata
- Download URL: claude_sock-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e484b6f62f24ed9636e3c3b5cfbb9cdc56dfea39fd1bc86dfd5ecf84f709217d
|
|
| MD5 |
5ffaed9112530883f988c0730b184d89
|
|
| BLAKE2b-256 |
bfdf91ab4c7974d83907f389a3c55f9d26f74410c7657f16a5c6975b37a6ae4f
|