# Morph SDK

Production-ready tools for AI coding agents: WarpGrep (intelligent code search), Fast Apply (10,500 tokens/s), and Repo Storage.

[![npm version](https://img.shields.io/npm/v/@morphllm/morphsdk.svg)](https://www.npmjs.com/package/@morphllm/morphsdk)

## Install

```bash
npm install @morphllm/morphsdk
```

Get your API key: [morphllm.com/dashboard/api-keys](https://morphllm.com/dashboard/api-keys)

```bash
export MORPH_API_KEY="sk-your-key-here"
```

## Features

- **🔍 WarpGrep** - Intelligent code search agent that explores your codebase with parallel grep/read operations
- **⚡ Fast Apply** - 98% 1st pass accuracy, AI-powered code editing at 10,500 tokens/s
- **📦 Repo Storage** - Agent native git with automatic code indexing and agent metadata
- **🤖 Agent Tools** - Ready-to-use tools for Anthropic, OpenAI, Gemini, and Vercel AI SDK

## Quick Start

### WarpGrep - Intelligent Code Search

WarpGrep is a search subagent that explores your codebase using parallel grep and file read operations. It understands natural language queries and returns relevant code with line numbers.

```typescript
import { WarpGrepClient } from '@morphllm/morphsdk/tools/warp-grep';

const client = new WarpGrepClient({ morphApiKey: process.env.MORPH_API_KEY });

const result = await client.execute({
  query: 'Find where authentication requests are handled',
  repoRoot: './my-project'
});

// Returns relevant files with specific line ranges
result.files.forEach(file => {
  console.log(`${file.path}: lines ${file.lines}`);
});
```

### As Agent Tool

```typescript
import { createWarpGrepTool } from '@morphllm/morphsdk/tools/warp-grep/anthropic';
import Anthropic from '@anthropic-ai/sdk';

const client = new Anthropic();
const tool = createWarpGrepTool({ repoRoot: './my-project' });

const response = await client.messages.create({
  model: "claude-sonnet-4-5-20250929",
  tools: [tool],
  messages: [{ role: "user", content: "Find the error handling logic" }]
});
```

### Remote Sandbox Support

WarpGrep works in remote sandboxes (E2B, Modal, Daytona) by providing custom command implementations:

```typescript
import { createWarpGrepTool } from '@morphllm/morphsdk/tools/warp-grep/anthropic';

const tool = createWarpGrepTool({
  repoRoot: '/home/repo',
  remoteCommands: {
    grep: async (pattern, path) => (await sandbox.run(`rg '${pattern}' '${path}'`)).stdout,
    read: async (path, start, end) => (await sandbox.run(`sed -n '${start},${end}p' '${path}'`)).stdout,
    listDir: async (path, maxDepth) => (await sandbox.run(`find '${path}' -maxdepth ${maxDepth}`)).stdout,
  },
});
```

## Fast Apply

AI-powered code editing at 10,500 tokens/s with 98% first-pass accuracy.

```typescript
import { MorphClient } from '@morphllm/morphsdk';

const morph = new MorphClient({ apiKey: process.env.MORPH_API_KEY });

await morph.fastApply.execute({
  target_filepath: 'src/app.ts',
  instructions: 'Add error handling to the API call',
  code_edit: `
    // ... existing code ...
    try {
      const response = await fetch(url);
      // ... existing code ...
    } catch (e) {
      console.error('API call failed:', e);
      throw e;
    }
  `
});
```

See [tools/fastapply/README.md](./tools/fastapply/README.md) for details.

## Repo Storage

Git built for AI agents with automatic code indexing.

```typescript
import { MorphClient } from '@morphllm/morphsdk';

const morph = new MorphClient({ apiKey: process.env.MORPH_API_KEY });

// Initialize repo
await morph.git.init({ repoId: 'my-project', dir: './my-project' });

// Stage and commit with agent metadata
await morph.git.add({ dir: './my-project', filepath: '.' });
await morph.git.commit({
  dir: './my-project',
  message: 'Add authentication',
  chatHistory: [
    { role: 'user', content: 'Add OAuth login' },
    { role: 'assistant', content: 'Adding Google OAuth...' }
  ]
});

// Push
await morph.git.push({ dir: './my-project' });

// Clone, branch, checkout
await morph.git.clone({ repoId: 'my-project', dir: './local-copy' });
await morph.git.branch({ dir: './my-project', name: 'feature' });
await morph.git.checkout({ dir: './my-project', ref: 'main' });
```

## Agent Tools

Ready-to-use tools for popular AI frameworks:

| Framework | Import Path |
|-----------|-------------|
| Anthropic SDK | `@morphllm/morphsdk/tools/warp-grep/anthropic` |
| OpenAI SDK | `@morphllm/morphsdk/tools/warp-grep/openai` |
| Gemini SDK | `@morphllm/morphsdk/tools/warp-grep/gemini` |
| Vercel AI SDK | `@morphllm/morphsdk/tools/warp-grep/vercel` |

Available tools:
- `createWarpGrepTool` - Intelligent code search
- `createFastApplyTool` - AI-powered code editing
- `createBrowserTool` - Browser automation

## Documentation

Full docs: [docs.morphllm.com](https://docs.morphllm.com)

**Key Pages:**
- [WarpGrep](https://docs.morphllm.com/sdk/components/warp-grep) - Intelligent code search
- [Fast Apply](https://docs.morphllm.com/sdk/components/fast-apply) - AI-powered code editing
- [Repo Storage](https://docs.morphllm.com/sdk/components/git) - Git operations and agent metadata
