Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
cb2a2f7
Revise supported frameworks and protocols sections
NathanTarbert Oct 31, 2025
a136965
Update image and enhance README formatting
NathanTarbert Nov 3, 2025
168a380
Add workflow to autoapprove community PRs (#636)
maxkorp Nov 5, 2025
54f5c41
Addressing regression in ADK 0.3.2 (#626)
contextablemark Nov 5, 2025
fba25db
0.0.40 (#632)
mme Nov 6, 2025
1857c0a
fix(langgraph): handle Command tool output (#639)
xdave Nov 6, 2025
913c18b
make sidebar nav maintain query string params (#649)
maxkorp Nov 6, 2025
4a2627a
ag-ui support claude agent python sdk
Nov 7, 2025
b698e5b
Merge remote-tracking branch 'origin/main'
Nov 7, 2025
970c2db
fix: dump safe json whenever string is required in lg-python fastapi …
ranst91 Nov 7, 2025
079169d
feature: implement claude agent sdk typescript version
Nov 7, 2025
a3c0fe1
chore(docs): Add the frameworks, protocols, and clients section in th…
NathanTarbert Nov 7, 2025
e7df7c6
fix: fix claude code agent sdk protocal transform error
Nov 8, 2025
05a3ec1
feature: finish claude agent sdk ts middleware
Nov 8, 2025
5c57b10
Merge branch 'main' of https://github.com/ag-ui-protocol/ag-ui
Nov 8, 2025
c98f5dd
chore: remove outdated ADK Middleware implementation documentation an…
Nov 9, 2025
c57eb1e
chore: update example environment file for Claude Agent SDK by removi…
Nov 10, 2025
c26363f
chore: update .gitignore to include all .env.local files and remove b…
Nov 10, 2025
8b19f36
chore: add .gitignore to prevent future .env.local commits
Nov 10, 2025
584e17b
chore: merge origin/main and resolve conflicts
Nov 10, 2025
a8b76dd
chore: restore pnpm-lock.yaml files in subdirectories
Nov 10, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ node_modules
.pnpm-store

**/.poetry-cache
**/.env.local
integrations/claude-agent-sdk/**/.env.local
10 changes: 10 additions & 0 deletions integrations/claude-agent-sdk/python/.env.local.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Claude Agent SDK Integration - Environment Variables
# Copy this file to .env.local and fill in your actual values

# Anthropic API Key (required for real API tests)
# Get your API key from: https://console.anthropic.com/
# ANTHROPIC_API_KEY=your-anthropic-api-key-here

# Optional for third party service
# ANTHROPIC_AUTH_TOKEN=third-party-auth-token
# ANTHROPIC_BASE_URL=third-party-base-url
31 changes: 31 additions & 0 deletions integrations/claude-agent-sdk/python/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Environment files
.env
.env.local
.env*.local

# Dependencies
node_modules/

# Build output
dist/
build/

# Logs
logs
*.log
npm-debug.log*
pnpm-debug.log*
yarn-debug.log*
yarn-error.log*

# OS
.DS_Store
Thumbs.db

# IDE
.vscode/
.idea/
*.swp
*.swo
*~

113 changes: 113 additions & 0 deletions integrations/claude-agent-sdk/python/ARCHITECTURE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Claude Agent SDK Integration Architecture

This document describes the architecture and design of the Claude Agent SDK integration that bridges Claude agents with the AG-UI Protocol.

## High-Level Architecture

```
AG-UI Protocol Claude Middleware Claude Agent SDK
│ │ │
RunAgentInput ──────> ClaudeAgent.run() ──────> SDK Client/Query
│ │ │
│ EventTranslator │
│ │ │
BaseEvent[] <──────── translate events <──────── Response[]
```

## Core Components

### ClaudeAgent (`claude_agent.py`)
The main orchestrator that:
- Manages agent lifecycle and session state
- Handles the bridge between AG-UI Protocol and Claude SDK
- Coordinates tool execution
- Supports both persistent sessions and stateless query mode

### EventTranslator (`event_translator.py`)
Converts between event formats:
- Claude SDK responses → AG-UI protocol events (16 standard event types)
- Maintains proper message boundaries
- Handles streaming text content
- Per-execution instances for thread safety

### SessionManager (`session_manager.py`)
Singleton pattern for centralized session control:
- Automatic session cleanup with configurable timeouts
- Session isolation per user
- Message tracking to avoid duplicates
- State management

### ToolAdapter (`tool_adapter.py`)
Tool format conversion:
- AG-UI Tool → Claude SDK tool format
- Tool call extraction and parsing
- Long-running tool detection

### ExecutionState (`execution_state.py`)
Tracks background Claude executions:
- Manages asyncio tasks running Claude SDK calls
- Event queue for streaming results
- Execution timing and completion tracking
- Tool call state management

## Event Flow

1. **Client Request**: AG-UI Protocol `RunAgentInput` received
2. **Session Resolution**: SessionManager finds or creates session
3. **Message Processing**: Unseen messages identified and processed
4. **Agent Execution**: Claude SDK called with messages and tools
5. **Event Translation**: Claude responses converted to AG-UI events
6. **Streaming Response**: Events streamed back via SSE or other transport

## Key Design Patterns

### Session Management
- **Persistent Mode**: Uses ClaudeSDKClient for session continuity
- **Stateless Mode**: Uses query() method with manual context management

### Tool Handling
- **Client Tools**: Long-running tools executed on frontend
- **Backend Tools**: Synchronous tools executed on backend
- **Tool Results**: Handled through message routing

### Event Streaming
- Background execution with event queue
- Non-blocking async/await throughout
- Proper cleanup on errors or timeouts

## Thread Safety

- Per-execution EventTranslator instances
- Singleton SessionManager with proper locking
- Isolated execution states per thread
- Thread-safe event queues

## Error Handling

- RunErrorEvent for various failure scenarios
- Proper async exception handling
- Resource cleanup on errors
- Timeout management at multiple levels

## Performance Considerations

- Async/await throughout for non-blocking operations
- Event streaming for real-time responses
- Configurable concurrent execution limits
- Automatic stale execution cleanup
- Efficient event queue management

## Implementation Notes

✅ **Implementation Complete**: The implementation has been updated based on the actual [Claude Agent SDK API](https://docs.claude.com/zh-CN/api/agent-sdk/python#claudesdkclient).

Key implementation details:

1. **SDK Initialization**: ✅ Implemented `_get_claude_client()` with `ClaudeSDKClient` and `query()` support
2. **Message Format**: ✅ Implemented prompt extraction (`_extract_user_prompt()`) for Claude SDK string-based API
3. **Response Handling**: ✅ Implemented `_call_claude_sdk()` supporting both persistent and stateless modes
4. **Tool Format**: ✅ Implemented `ToolAdapter` with `SdkMcpTool` and `create_sdk_mcp_server()`
5. **Event Translation**: ✅ Implemented `EventTranslator` handling `Message`, `AssistantMessage`, `TextBlock`, `ToolUseBlock`, `ToolResultBlock`

The implementation follows the actual Claude Agent SDK patterns and should work with the real SDK. Some fine-tuning may be needed based on real-world testing.

Loading