@mcp-b/embedded-agent 0.0.4 → 0.0.5

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.
package/README.md CHANGED
@@ -1,29 +1,330 @@
1
- # react-compiler-components-starter
1
+ # @mcp-b/embedded-agent
2
2
 
3
- A starter for creating a React component library with React Compiler.
3
+ React components for embedding an AI agent UI with MCP (Model Context Protocol) tool support and voice mode.
4
4
 
5
- ## Development
5
+ ## Architecture Overview
6
6
 
7
- - Install dependencies:
7
+ The package follows a **core/widgets** architecture:
8
+
9
+ - **Core** (`src/core/`): Data layer - providers and hooks, no UI
10
+ - **Widgets** (`src/widgets/`): UI layer - independent components that consume core
8
11
 
9
- ```bash
10
- npm install
12
+ ```
13
+ src/
14
+ ├── core/ # DATA LAYER
15
+ │ ├── providers/
16
+ │ │ ├── AgentProvider.tsx # Unified provider (START HERE)
17
+ │ │ └── index.ts
18
+ │ ├── hooks/
19
+ │ │ ├── useAgent.ts # Main facade hook (START HERE)
20
+ │ │ └── index.ts
21
+ │ └── index.ts
22
+
23
+ ├── widgets/ # UI LAYER
24
+ │ ├── pill/ # Compact floating UI
25
+ │ │ └── index.ts # Exports AgentPill, PillContainer, etc.
26
+ │ ├── modal/ # Full chat modal
27
+ │ │ └── index.ts # Exports AssistantModal, Thread, etc.
28
+ │ ├── shared/ # Shared components
29
+ │ │ └── index.ts # Exports ActionList, SummaryBlock, etc.
30
+ │ └── index.ts
31
+
32
+ ├── components/ # IMPLEMENTATION (internal)
33
+ │ ├── pill/ # Pill component implementations
34
+ │ │ ├── AgentPill.tsx # Main pill component
35
+ │ │ ├── PillContainer.tsx # Morphing container
36
+ │ │ ├── PillVoice.tsx # Voice mode UI
37
+ │ │ ├── ActionList.tsx # Action display
38
+ │ │ └── ...
39
+ │ └── ... # Other component implementations
40
+
41
+ ├── hooks/ # INDIVIDUAL HOOKS (internal)
42
+ │ ├── useActions.ts # Derives actions from tool calls
43
+ │ ├── useVoiceActions.ts # Voice mode action tracking
44
+ │ ├── useVoiceSummary.ts # Voice session summary
45
+ │ ├── useVoiceMode.ts # Voice mode state machine
46
+ │ └── ...
47
+
48
+ ├── providers/ # INDIVIDUAL PROVIDERS (internal)
49
+ │ ├── MCPToolsProvider.tsx # MCP tool registry
50
+ │ ├── VoiceModeProvider.tsx # Voice mode context
51
+ │ └── VoiceMCPBridge.tsx # Connects MCP tools to voice
52
+
53
+ ├── services/ # EXTERNAL SERVICES
54
+ │ └── realtime/ # OpenAI Realtime API via WebRTC
55
+ │ ├── openai-realtime-service.ts
56
+ │ ├── webrtc-manager.ts
57
+ │ └── ...
58
+
59
+ ├── lib/ # UTILITIES
60
+ │ ├── constants.ts # Magic numbers live here
61
+ │ ├── utils.ts # cn(), etc.
62
+ │ └── ...
63
+
64
+ └── index.ts # PUBLIC EXPORTS
11
65
  ```
12
66
 
13
- - Run the playground:
67
+ ## Quick Start
14
68
 
15
- ```bash
16
- npm run play
69
+ ### Recommended: AgentProvider + Widget
70
+
71
+ ```tsx
72
+ import { AgentPill, AgentProvider } from '@mcp-b/embedded-agent'
73
+
74
+ function App() {
75
+ return (
76
+ <AgentProvider apiBase="https://your-worker.workers.dev">
77
+ <AgentPill position="bottom-center" showVoiceButton />
78
+ </AgentProvider>
79
+ )
80
+ }
17
81
  ```
18
82
 
19
- - Run the unit tests:
83
+ ### Custom UI with useAgent
20
84
 
21
- ```bash
22
- npm run test
85
+ ```tsx
86
+ import { AgentProvider, useAgent } from '@mcp-b/embedded-agent'
87
+
88
+ function CustomUI() {
89
+ const agent = useAgent()
90
+
91
+ return (
92
+ <div>
93
+ {agent.isRunning && <p>Processing...</p>}
94
+ {agent.activeActions.map((action) => (
95
+ <div key={action.id}>{action.label}</div>
96
+ ))}
97
+ {agent.voice?.isActive && <button onClick={agent.voice.stop}>End Voice</button>}
98
+ </div>
99
+ )
100
+ }
101
+
102
+ function App() {
103
+ return (
104
+ <AgentProvider apiBase="https://...">
105
+ <CustomUI />
106
+ </AgentProvider>
107
+ )
108
+ }
109
+ ```
110
+
111
+ ## Core API
112
+
113
+ ### AgentProvider
114
+
115
+ **File:** `src/core/providers/AgentProvider.tsx`
116
+
117
+ Unified provider that sets up:
118
+
119
+ - MCP tool registry (`MCPToolsProvider`)
120
+ - Voice mode bridge (`VoiceMCPBridge`)
121
+ - Chat runtime (`AssistantRuntimeProvider` from @assistant-ui/react)
122
+
123
+ ```tsx
124
+ interface AgentProviderProps {
125
+ children: ReactNode
126
+ apiBase?: string // Backend URL
127
+ tokenEndpoint?: string // Voice token endpoint (defaults to {apiBase}/api/realtime/session)
128
+ autoConnectLocal?: boolean // Auto-connect to local MCP source (default: true)
129
+ onToolsChange?: (tools: ToolWithSource[]) => void
130
+ onVoiceError?: (error: string) => void
131
+ onVoiceConnect?: () => void
132
+ onVoiceDisconnect?: (duration: number) => void
133
+ }
134
+ ```
135
+
136
+ ### useAgent Hook
137
+
138
+ **File:** `src/core/hooks/useAgent.ts`
139
+
140
+ Main facade hook - combines all agent capabilities into one interface.
141
+
142
+ ```tsx
143
+ interface AgentState {
144
+ // Thread
145
+ messages: ReadonlyArray<ThreadMessage>
146
+ isRunning: boolean
147
+ hasMessages: boolean
148
+
149
+ // Actions (from tool calls)
150
+ actions: Action[] // All text mode actions
151
+ currentAction: Action | null // Currently running
152
+ recentActions: Action[] // Last 3 completed
153
+
154
+ // Voice mode actions
155
+ voiceActions: Action[] // Actions from voice mode
156
+ activeActions: Action[] // Voice or text depending on mode
157
+
158
+ // Summary
159
+ summary: string | null // Latest summary text
160
+ voiceSummary: VoiceSummary | null
161
+
162
+ // Voice controls (null if not configured)
163
+ voice: {
164
+ isActive: boolean
165
+ isConnecting: boolean
166
+ isError: boolean
167
+ isMuted: boolean
168
+ error?: string
169
+ audioLevel?: AudioLevelData
170
+ transcript?: TranscriptData
171
+ start: () => Promise<void>
172
+ stop: () => void
173
+ toggleMute: (muted?: boolean) => void
174
+ sendMessage: (text: string) => void
175
+ } | null
176
+
177
+ // MCP tools (null if not in context)
178
+ tools: {
179
+ list: ToolWithSource[]
180
+ call: (name: string, args: Record<string, unknown>) => Promise<CallToolResult>
181
+ } | null
182
+
183
+ // Derived
184
+ isVoiceActive: boolean
185
+ }
186
+ ```
187
+
188
+ ## Widgets
189
+
190
+ ### AgentPill
191
+
192
+ **File:** `src/components/pill/AgentPill.tsx`
193
+
194
+ Compact, morphing floating UI. Shows actions, voice mode, and composer.
195
+
196
+ ```tsx
197
+ interface AgentPillProps {
198
+ position?: 'bottom-center' | 'bottom-right'
199
+ onOpenHistory?: () => void
200
+ showVoiceButton?: boolean
201
+ autoCollapse?: boolean // Auto-collapse after 30s inactivity
202
+ className?: string
203
+ }
204
+ ```
205
+
206
+ ### AssistantModal
207
+
208
+ **File:** `src/components/assistant-modal.tsx`
209
+
210
+ Traditional chat modal with full message thread.
211
+
212
+ ```tsx
213
+ // No props - uses context from AgentProvider
214
+ <AssistantModal />
23
215
  ```
24
216
 
25
- - Build the library:
217
+ ## Key Implementation Details
218
+
219
+ ### Action Tracking
220
+
221
+ **Files:** `src/hooks/useActions.ts`, `src/hooks/useVoiceActions.ts`
222
+
223
+ Actions are derived from tool calls in assistant messages. The `useActions` hook:
224
+
225
+ 1. Subscribes to thread messages via `useThread`
226
+ 2. Extracts tool calls from assistant messages
227
+ 3. Maps tool calls to Action objects with status (running/success/error)
228
+
229
+ Voice actions work similarly but track tool calls from the realtime voice API.
230
+
231
+ ### Voice Mode
232
+
233
+ **Files:**
234
+
235
+ - `src/hooks/useVoiceMode.ts` - State machine for voice sessions
236
+ - `src/services/realtime/` - OpenAI Realtime API integration
237
+ - `src/providers/VoiceMCPBridge.tsx` - Connects MCP tools to voice mode
238
+
239
+ Voice mode uses WebRTC to connect to OpenAI's Realtime API. The VoiceMCPBridge:
240
+
241
+ 1. Watches MCP tools from MCPToolsProvider
242
+ 2. Converts them to RegisteredTool format
243
+ 3. Provides tool executor that calls through MCP
244
+
245
+ ### MCP Tools
246
+
247
+ **File:** `src/providers/MCPToolsProvider.tsx`
248
+
249
+ Manages MCP tool sources (local tab, remote servers). Key exports:
250
+
251
+ - `useMCPTools()` - Required context hook
252
+ - `useOptionalMCPTools()` - Returns null if not in provider
253
+
254
+ ### Constants
255
+
256
+ **File:** `src/lib/constants.ts`
257
+
258
+ All magic numbers are centralized here:
259
+
260
+ - `VOICE_ACTIONS_RETENTION_MS` (3000ms) - How long voice actions persist
261
+ - `VOICE_SUMMARY_RETENTION_MS` (30000ms) - How long voice summary persists
262
+ - `TOOL_CALL_DISPLAY_DURATION_MS` (2000ms) - Tool status display time
263
+ - etc.
264
+
265
+ ## Legacy API
266
+
267
+ The `EmbeddedAgent` component is preserved for backward compatibility but wraps the new architecture internally.
268
+
269
+ ```tsx
270
+ // Legacy (still works)
271
+ import { EmbeddedAgent } from '@mcp-b/embedded-agent'
272
+
273
+ <EmbeddedAgent
274
+ appId="your-app"
275
+ apiBase="https://..."
276
+ viewMode="pill"
277
+ />
278
+
279
+ // New (recommended)
280
+ import { AgentProvider, AgentPill } from '@mcp-b/embedded-agent'
281
+
282
+ <AgentProvider apiBase="https://...">
283
+ <AgentPill />
284
+ </AgentProvider>
285
+ ```
286
+
287
+ ## Development
26
288
 
27
289
  ```bash
28
- npm run build
290
+ # Build
291
+ pnpm build
292
+
293
+ # Type check
294
+ pnpm check:types
295
+
296
+ # Lint
297
+ pnpm check:lint
298
+
299
+ # Test
300
+ pnpm test
301
+
302
+ # Run playground
303
+ pnpm play
29
304
  ```
305
+
306
+ ## Exports Reference
307
+
308
+ ### From `@mcp-b/embedded-agent`
309
+
310
+ **Core:**
311
+
312
+ - `AgentProvider`, `AgentProviderProps`
313
+ - `useAgent`, `AgentState`, `AgentVoice`, `AgentTools`
314
+ - `useActions`, `useCurrentAction`, `useRecentActions`, `Action`
315
+ - `useVoiceActions`, `useVoiceSummary`, `VoiceSummary`
316
+ - `useMCPTools`, `useOptionalMCPTools`, `MCPToolsContextValue`
317
+
318
+ **Widgets:**
319
+
320
+ - `AgentPill`, `AgentPillProps`, `PillPosition`
321
+ - `AssistantModal`
322
+ - `ActionList`, `SummaryBlock`, `CurrentActivity`
323
+ - `PillContainer`, `PillComposer`, `PillVoice`
324
+ - `LiveWaveform`, `VoiceIndicator`, `ToolStatusBorder`
325
+
326
+ **Legacy:**
327
+
328
+ - `EmbeddedAgent`, `EmbeddedAgentProps`
329
+
330
+ See `src/index.ts` for the complete export list.