How unifai-network/unifai-sdk-js works
Project Type and Purpose
• This is a client SDK library (JavaScript/TypeScript) for the UnifAI platform.
• It exposes two main entrypoints:
• Tools – to call dynamic/static tools in agents
• Toolkit – to create and run custom toolkits (action handlers)
• It also includes an MCP server component for Model Context Protocol clients.
File Structure
• Top‐level:
• README, LICENSE, package.json, tsconfig.json – metadata & build config
• examples/ – sample code demonstrating both Tools and Toolkit usage
• src/
• common/ – low‐level API client (HTTP, auth, constants)
• toolkit/ – Toolkit class, context, messages, registration, run loop
• tools/ – Tools class (getTools, callTools) + MCP server under tools/mcp
• index.ts – central export of public API
• .github/workflows/release.yml – CI for publishing releases
Extract Architectural Insights from README
• Two API keys (Agent vs Toolkit)
• Two usage patterns:
a. Using Tools in a client app → calls UnifAI REST endpoints, then optionally LLM APIs
b. Creating a Toolkit → registers local handlers, listens for incoming calls, responds via UnifAI
• MCP server hooks into any MCP‐compliant client (e.g., Claude Desktop) via a child process.
Main Components
a. Client Application (consumer code in examples or user’s app)
b. unifai-sdk-js Library, subdivided into:
• Common API Module
• Tools Module
• Toolkit Module
• MCP Server Module
c. UnifAI Platform (REST API endpoints for tools & toolkits)
d. External LLM API (OpenAI‐compatible endpoint or OpenRouter)
e. MCP Clients (e.g., Claude Desktop) when using MCP mode
Relationships & Data Flows
• Client App → imports unifai-sdk-js → invokes Tools.getTools / Tools.callTools
→ Common API Module → UnifAI Platform → returns tool metadata or results
• Client App → passes tool metadata to LLM API → receives function‐call response → back to Tools.callTools
• Client App → imports Toolkit → registers handlers → toolkit.run()
→ Common API Module subscribes to UnifAI event stream → invokes handler callbacks → POST results back
• Client App / MCP Client → spawns unifai-tools-mcp (MCP Server) → communicates over STDIN/STDOUT via MCP Protocol → MCP Server translates to Tools module calls → UnifAI Platform
Architectural Patterns & Design Principles
• Modular separation: common logic vs. Tools vs. Toolkit vs. MCP
• Dependency inversion: high‐level modules (Tools/Toolkit) depend on abstracted Common API
• Pub/sub style for toolkit event handling vs. request/response for Tools
• Process‐isolation for MCP server (child process)
Essential Details to Capture
• Two API key types and where they apply (agent vs. toolkit)
• Separation of flows: dynamic tools, static toolkits, static actions
• Role of the MCP server as a bridge to MCP clients
• Core config surfaces: environment vars for MCP (UNIFAI_DYNAMIC_TOOLS, etc.)