New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@coinbase/agentkit

Package Overview
Dependencies
Maintainers
9
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@coinbase/agentkit

Coinbase AgentKit core primitives

  • 0.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
9
Weekly downloads
 
Created
Source

Agentkit

AgentKit is a framework for easily enabling AI agents to take actions onchain. It is designed to be framework-agnostic, so you can use it with any AI framework, and wallet-agnostic, so you can use it with any wallet.

Table of Contents

Getting Started

Prerequisites:

Installation

npm install @coinbase/agentkit

Usage

Create an AgentKit instance. If no wallet or action providers are specified, the agent will use the CdpWalletProvider and WalletProvider action provider.

const agentKit = await AgentKit.from({
  cdpApiKeyName: "CDP API KEY NAME",
  cdpApiKeyPrivate: "CDP API KEY PRIVATE KEY",
});

Create an AgentKit instance with a specified wallet provider.

import { CdpWalletProvider } from "@coinbase/agentkit";

const walletProvider = await CdpWalletProvider.configureWithWallet({
    apiKeyName: "CDP API KEY NAME",
    apiKeyPrivate: "CDP API KEY PRIVATE KEY",
    networkId: "base-mainnet",
});

const agentKit = await AgentKit.from({
    walletProvider,
});

Create an AgentKit instance with a specified action providers.

import { cdpApiActionProvider, pythActionProvider } from "@coinbase/agentkit";

const agentKit = await AgentKit.from({
    walletProvider,
    actionProviders: [
        cdpApiActionProvider({
            apiKeyName: "CDP API KEY NAME",
            apiKeyPrivate: "CDP API KEY PRIVATE KEY",
        }),
        pythActionProvider(),
    ],
});

Use the agent's actions with a framework extension. For example, using LangChain + OpenAI.

Prerequisites:

npm install @langchain @langchain/langgraph @langchain/openai
import { getLangChainTools } from "@coinbase/agentkit-langchain";
import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { ChatOpenAI } from "@langchain/openai";

const tools = await getLangChainTools(agentKit);

const llm = new ChatOpenAI({
    model: "gpt-4o-mini",
});

const agent = createReactAgent({
    llm,
    tools,
});

Creating an Action Provider

Action providers are used to define the actions that an agent can take. They are defined as a class that extends the ActionProvider abstract class.

import { ActionProvider, WalletProvider, Network } from "@coinbase/agentkit";

// Define an action provider that uses a wallet provider.
class MyActionProvider extends ActionProvider<WalletProvider> {
    constructor() {
        super("my-action-provider", []);
    }

    // Define if the action provider supports the given network
    supportsNetwork = (network: Network) => true;
}

Adding Actions to your Action Provider

Actions are defined as instance methods on the action provider class with the @CreateAction decorator. Actions can use a wallet provider or not and always return a Promise that resolves to a string.

Required Typescript Compiler Options

Creating actions with the @CreateAction decorator requires the following compilerOptions to be included in your project's tsconfig.json.

{
    "compilerOptions": {
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true
    }
} 
Steps to create an action
  1. Define the action schema. Action schemas are defined using the zod library.
import { z } from "zod";

export const MyActionSchema = z.object({
  myField: z.string(),
});
  1. Define the action.
import { ActionProvider, WalletProvider, Network, CreateAction } from "@coinbase/agentkit";

class MyActionProvider extends ActionProvider<WalletProvider> {
    constructor() {
        super("my-action-provider", []);
    }

    @CreateAction({
        name: "my-action",
        description: "My action description",
        schema: MyActionSchema,
    })
    async myAction(args: z.infer<typeof MyActionSchema>): Promise<string> {
        return args.myField;
    }

    supportsNetwork = (network: Network) => true;
}

export const myActionProvider = () => new MyActionProvider();
Adding Actions to your Action Provider that use a Wallet Provider

Actions that use a wallet provider can be defined as instance methods on the action provider class with the @CreateAction decorator that have a WalletProvider as the first parameter.

class MyActionProvider extends ActionProvider<WalletProvider> {
    constructor() {
        super("my-action-provider", []);
    }

    @CreateAction({
        name: "my-action",
        description: "My action description",
        schema: MyActionSchema,
    })
    async myAction(walletProvider: WalletProvider, args: z.infer<typeof MyActionSchema>): Promise<string> {
        return walletProvider.signMessage(args.myField);
    }

    supportsNetwork = (network: Network) => true;
}

Adding an Action Provider to your AgentKit instance.

This gives your agent access to the actions defined in the action provider.

const agentKit = new AgentKit({
  cdpApiKeyName: "CDP API KEY NAME",
  cdpApiKeyPrivate: "CDP API KEY PRIVATE KEY",
  actionProviders: [myActionProvider()],
});

Wallet Providers

Wallet providers give an agent access to a wallet. AgentKit currently supports the following wallet providers:

EVM:

CdpWalletProvider

The CdpWalletProvider is a wallet provider that uses the Coinbase Developer Platform (CDP) API Wallet.

Network Configuration

The CdpWalletProvider can be configured to use a specific network by passing the networkId parameter to the configureWithWallet method. The networkId is the ID of the network you want to use. You can find a list of supported networks on the CDP API docs.

import { CdpWalletProvider } from "@coinbase/agentkit";

const walletProvider = await CdpWalletProvider.configureWithWallet({
    apiKeyName: "CDP API KEY NAME",
    apiKeyPrivate: "CDP API KEY PRIVATE KEY",
    networkId: "base-mainnet",
});
Configuring from an existing CDP API Wallet

If you already have a CDP API Wallet, you can configure the CdpWalletProvider by passing the wallet parameter to the configureWithWallet method.

import { CdpWalletProvider } from "@coinbase/agentkit";
import { Wallet } from "@coinbase/coinbase-sdk";
const walletProvider = await CdpWalletProvider.configureWithWallet({
    wallet,
    apiKeyName: "CDP API KEY NAME",
    apiKeyPrivate: "CDP API KEY PRIVATE KEY",
});
Configuring from a mnemonic phrase

The CdpWalletProvider can be configured from a mnemonic phrase by passing the mnemonicPhrase parameter to the configureWithWallet method.

import { CdpWalletProvider } from "@coinbase/agentkit";

const walletProvider = await CdpWalletProvider.configureWithWallet({
    mnemonicPhrase: "MNEMONIC PHRASE",
});
Exporting a wallet

The CdpWalletProvider can export a wallet by calling the exportWallet method.

import { CdpWalletProvider } from "@coinbase/agentkit";

const walletProvider = await CdpWalletProvider.configureWithWallet({
    mnemonicPhrase: "MNEMONIC PHRASE",
});

const walletData = await walletProvider.exportWallet();
Importing a wallet from WalletData JSON string

The CdpWalletProvider can import a wallet from a WalletData JSON string by passing the cdpWalletData parameter to the configureWithWallet method.

import { CdpWalletProvider } from "@coinbase/agentkit";

const walletProvider = await CdpWalletProvider.configureWithWallet({
    cdpWalletData: "WALLET DATA JSON STRING",
    apiKeyName: "CDP API KEY NAME",
    apiKeyPrivate: "CDP API KEY PRIVATE KEY",
});

ViemWalletProvider

The ViemWalletProvider is a wallet provider that uses the Viem library. It is useful for interacting with any EVM-compatible chain.

import { ViemWalletProvider } from "@coinbase/agentkit";
import { privateKeyToAccount } from "viem/accounts";
import { baseSepolia } from "viem/chains";
import { http } from "viem/transports";
import { createWalletClient } from "viem";

const account = privateKeyToAccount(
  "0x4c0883a69102937d6231471b5dbb6208ffd70c02a813d7f2da1c54f2e3be9f38",
);

const client = createWalletClient({
  account,
  chain: baseSepolia,
  transport: http(),
});

const walletProvider = new ViemWalletProvider(client);

Contributing

See CONTRIBUTING.md for more information.

Keywords

FAQs

Package last updated on 08 Feb 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc