@composio/anthropic
Non-Agentic Provider for Anthropic in Composio SDK.
Features
- Tool Integration: Seamless integration with Anthropic's tool calling capabilities
- Type Safety: Full TypeScript support with proper type definitions
- Tool Execution: Execute tools with proper parameter handling and response formatting
- Caching Support: Optional tool caching for improved performance
- Modifier Support: Support for execution modifiers to transform tool inputs and outputs
- Streaming Support: First-class support for streaming responses
Installation
npm install @composio/anthropic
yarn add @composio/anthropic
pnpm add @composio/anthropic
Environment Variables
Required environment variables:
COMPOSIO_API_KEY: Your Composio API key
ANTHROPIC_API_KEY: Your Anthropic API key
Optional environment variables:
ANTHROPIC_API_URL: Custom API base URL (defaults to Anthropic's API)
Quick Start
import { Composio } from '@composio/core';
import { AnthropicProvider } from '@composio/anthropic';
const composio = new Composio({
apiKey: 'your-composio-api-key',
provider: new AnthropicProvider({ cacheTools: true }),
});
const tools = await composio.tools.get('user123', {
toolkits: ['gmail', 'googlecalendar'],
limit: 10,
});
Examples
Check out our complete example implementations:
Basic Example
import { Composio } from '@composio/core';
import { AnthropicProvider } from '@composio/anthropic';
import Anthropic from '@anthropic-ai/sdk';
const anthropic = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
});
const composio = new Composio({
apiKey: process.env.COMPOSIO_API_KEY,
provider: new AnthropicProvider(),
});
const tools = await composio.tools.get('user123', {
toolkits: ['gmail'],
});
const message = await anthropic.messages.create({
model: 'claude-3-sonnet-20240229',
max_tokens: 1024,
tools: tools,
messages: [
{
role: 'user',
content: 'Send an email to support@example.com',
},
],
});
if (message.content[0].type === 'tool_calls') {
const toolCall = message.content[0];
const result = await composio.provider.executeToolCall('user123', toolCall, {
connectedAccountId: 'account123',
});
console.log('Tool execution result:', result);
}
Streaming Example
import { Composio } from '@composio/core';
import { AnthropicProvider } from '@composio/anthropic';
import Anthropic from '@anthropic-ai/sdk';
const anthropic = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
});
const composio = new Composio({
apiKey: process.env.COMPOSIO_API_KEY,
provider: new AnthropicProvider(),
});
const tools = await composio.tools.get('user123', {
toolkits: ['gmail'],
});
const stream = await anthropic.messages.stream({
model: 'claude-3-sonnet-20240229',
max_tokens: 1024,
tools: tools,
messages: [
{
role: 'user',
content: 'Send an email to support@example.com',
},
],
});
for await (const messageChunk of stream) {
if (messageChunk.type === 'content_block_delta' && messageChunk.delta.type === 'tool_calls') {
const result = await composio.provider.executeToolCall('user123', messageChunk.delta, {
connectedAccountId: 'account123',
});
console.log('Tool execution result:', result);
} else {
console.log('Message chunk:', messageChunk);
}
}
Provider Configuration
The Anthropic provider can be configured with various options:
const provider = new AnthropicProvider({
cacheTools: true,
modifiers: {
beforeExecute: params => {
return params;
},
afterExecute: response => {
return response;
},
},
});
API Reference
AnthropicProvider Class
The AnthropicProvider class extends BaseNonAgenticProvider and provides Anthropic-specific functionality.
Constructor
new AnthropicProvider(options?: { cacheTools?: boolean })
options.cacheTools: Optional boolean to enable tool caching (default: false)
Methods
wrapTool(tool: Tool): AnthropicTool
Wraps a Composio tool in the Anthropic format.
const tool = provider.wrapTool(composioTool);
wrapTools(tools: Tool[]): AnthropicToolCollection
Wraps multiple Composio tools in the Anthropic format.
const tools = provider.wrapTools(composioTools);
executeToolCall(userId: string, toolUse: AnthropicToolUseBlock, options?: ExecuteToolFnOptions, modifiers?: ExecuteToolModifiers): Promise<string>
Executes a tool call from Anthropic and returns the result as a string.
const result = await composio.provider.executeToolCall(
'user123',
{
type: 'tool_use',
id: 'tu_123',
name: 'tool-name',
input: { param: 'value' },
},
{
connectedAccountId: 'account123',
customAuthParams: {
},
},
{
beforeExecute: params => params,
afterExecute: response => response,
}
);
handleToolCalls(userId: string, message: Message, options?: ExecuteToolFnOptions, modifiers?: ExecuteToolModifiers): Promise<Anthropic.Messages.MessageParam[]>
Processes and executes all tool calls found in an Anthropic message response. This method automatically extracts tool calls from the message content, executes them, and returns their results.
Parameters:
userId (string): The ID of the user making the tool calls
message (Message): The Anthropic message response containing tool calls
options (optional): Additional options for tool execution
connectedAccountId: ID of the connected account
customAuthParams: Custom authentication parameters
modifiers (optional): Functions to modify tool execution
beforeExecute: Transform parameters before execution
afterExecute: Transform response after execution
Returns:
Promise<Anthropic.Messages.MessageParam[]>: Tool execution results for the assistant message
Example:
const message = {
id: 'msg_123',
content: [
{ type: 'text', text: 'Hello' },
{
type: 'tool_use',
id: 'tu_123',
name: 'test-tool',
input: { param: 'value' },
},
{
type: 'tool_use',
id: 'tu_456',
name: 'another-tool',
input: { param: 'value2' },
},
],
};
const results = await provider.handleToolCalls(
'user123',
message,
{
connectedAccountId: 'account123',
customAuthParams: {
parameters: [{ name: 'token', value: 'abc123', in: 'header' }],
},
},
{
beforeExecute: params => params,
afterExecute: response => response,
}
);
Contributing
We welcome contributions! Please see our Contributing Guide for more details.
License
ISC License
Support
For support, please visit our Documentation or join our Discord Community.