Skip to content

Let agent run MCP and other tool calls using code instead. Inspired by Cloudflare's Code Mode, but runs locally.

Notifications You must be signed in to change notification settings

CryptoKass/codeable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Codeable

Codeable is a tool that lets you wrap existing tools into a code-writing environment. Instead of relying on an LLM to make multiple sequential tool calls (which can be fragile and token-expensive), Codeable allows the agent to write a single script that orchestrates these tools to achieve a goal.

Note: This concept is inspired by Cloudflare's Code Mode, but Codeable runs locally in your environment rather than on a Cloudflare Worker.

Why Codeable?

AI agents are often better at writing code than they are at managing complex tool calling chains.

  • Standard Tool Calling: The LLM must decide to call a tool, wait for the result, feed it back into context, and decide the next step. This involves multiple round-trips and context switching.
  • Codeable: The LLM writes a standard TypeScript/JavaScript function that calls the tools directly. It can use loops, variables, and logic natively. The code is then executed safely in your local environment.

Installation

npm install codeable
# or
pnpm add codeable

Usage

Codeable is designed to work seamlessly with the Vercel AI SDK.

1. Define Your Tools

First, define the tools your agent needs using the AI SDK's tool() function.

import { z } from "zod";
import { tool } from "ai";

const weatherTool = tool({
  description: "Get the weather for a location",
  inputSchema: z.object({ location: z.string() }),
  outputSchema: z.object({ temperature: z.number(), condition: z.string() }),
  execute: async ({ location }) => {
    // Mock implementation
    return { temperature: 72, condition: "Sunny" };
  },
});

2. Create a Codeable Instance

Wrap your tools using the codeable helper.

import { openai } from "@ai-sdk/openai";
import { codeable } from "codeable/ai-sdk";

const myCodeable = codeable({
  systemPrompt: "You are a helpful assistant.",
  llm: openai("gpt-4o"), // Model used to write the code
  tools: {
    weather: weatherTool,
    // ... add other tools here
  },
});

3. Use with Vercel AI SDK

Pass the codeable tool and its system prompt to your AI generation function.

import { streamText } from "ai";

const result = await streamText({
  model: openai("gpt-4o"),
  // The codeable system prompt helps the model understand available tools
  system: myCodeable.system,
  tools: {
    codeable: myCodeable.tool, // Expose the meta-tool
  },
  prompt:
    "Check the weather in Tokyo and New York, then tell me which is warmer.",
});

How It Works

  1. Prompting: When you ask a question, the LLM (via codeable) receives a description of all available tools as TypeScript definitions.
  2. Code Generation: Instead of calling weather directly, the LLM writes a script:
    async function main({ tools }) {
      const tokyo = await tools.weather({ location: "Tokyo" });
      const ny = await tools.weather({ location: "New York" });
      return tokyo.temperature > ny.temperature ? "Tokyo" : "New York";
    }
  3. Execution: Codeable executes this script locally, handling the tool calls and returning the final result to the main agent.

About

Let agent run MCP and other tool calls using code instead. Inspired by Cloudflare's Code Mode, but runs locally.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published