Skip to content

Repository files navigation

Custom Coffee Claude Agent

A custom AI agent built with the Claude Agent SDK featuring OAuth authentication, custom tools, and streaming chat interface.

Features

  • OAuth Authentication: Uses Claude subscription instead of API keys
  • OpenAI-Compatible API: REST API server with streaming support
  • Custom MCP Tools: Coffee shop price calculator tool
  • Streaming Chat: Interactive CLI with color-coded output
  • File Operations: Read, write, edit files in your project
  • Tool Permissions: Granular control over allowed tools

Setup

Prerequisites

  • Node.js 18+
  • pnpm
  • Claude subscription (no API key needed)

Installation

pnpm install

Authentication

The agent uses OAuth authentication automatically from your Claude Code CLI setup.

First time setup:

If you haven't used Claude Code CLI before, authenticate first:

# Install Claude Code CLI globally (if not already installed)
npm install -g @anthropic-ai/claude-code

# Login via OAuth
claude login

This opens a browser window to authenticate with your Claude account. Once authenticated, you can use this custom agent.

No API key needed! The SDK automatically uses your Claude Code CLI authentication.

Usage

CLI Mode

Run the interactive chat agent:

pnpm dev

Once running, you can:

  • Ask questions
  • Request file operations
  • Use the custom coffee price calculator tool
  • Type exit to quit

Example Prompts

How many files are in this project?
What is the price of a medium oat milk latte?
Read the package.json file

API Mode

Run the OpenAI-compatible API server:

# Development mode (auto-reload on changes)
pnpm api:dev

# Production mode
pnpm api

The server runs on port 8130 by default (configurable in .env).

Configuration

Update your .env file:

PORT=8130
API_KEY=your-secret-api-key  # Change this to a secure key!

API Endpoints

  • POST /v1/chat/completions - OpenAI-compatible chat completions
  • GET /health - Health check endpoint

Using the API with curl

Non-Streaming Request (Recommended):

curl http://localhost:8130/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer your-secret-api-key" \
  -d '{
    "model": "custom-claude-agent",
    "messages": [
      {"role": "user", "content": "What files are in this project?"}
    ]
  }' | jq -r '.choices[0].message.content'

Streaming Request:

curl http://localhost:8130/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer your-secret-api-key" \
  -d '{
    "model": "custom-claude-agent",
    "messages": [
      {"role": "user", "content": "What files are in this project?"}
    ],
    "stream": true
  }' | grep "^data: " | sed 's/^data: //' | grep -v "\[DONE\]" | jq -r '.choices[0].delta.content // empty'

With System Message:

curl http://localhost:8130/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer your-secret-api-key" \
  -d '{
    "model": "custom-claude-agent",
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "What is the price of a large latte?"}
    ]
  }' | jq -r '.choices[0].message.content'

Using with OpenAI Client Libraries

Python:

from openai import OpenAI

client = OpenAI(
    api_key="your-secret-api-key",
    base_url="http://localhost:8130/v1"
)

response = client.chat.completions.create(
    model="custom-claude-agent",
    messages=[
        {"role": "user", "content": "What files are in this project?"}
    ]
)

print(response.choices[0].message.content)

JavaScript/TypeScript:

import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: 'your-secret-api-key',
  baseURL: 'http://localhost:8130/v1'
});

const response = await client.chat.completions.create({
  model: 'custom-claude-agent',
  messages: [
    { role: 'user', content: 'What is the price of a large oat milk latte?' }
  ]
});

console.log(response.choices[0].message.content);

Custom Tools

The agent includes a custom MCP tool for calculating coffee prices:

  • Tool: calculate_coffee_price
  • Inputs: size, drink type, extras, currency
  • Example: "What's the price of a large latte with oat milk?"

Configuration

Allowed Tools

Edit src/index.ts to modify which tools the agent can use:

allowedTools: ['Read', 'Glob', 'Bash', 'Grep', `mcp__${customServer.name}__*`]

System Prompt

Customize the agent's behavior by editing the customSystemPrompt in src/index.ts.

Add More Custom Tools

Add new tools in src/tools.ts using the tool() function:

const myTool = tool({
  name: 'my_tool',
  description: 'What this tool does',
  inputSchema: z.object({
    // Define inputs with Zod
  }),
  handler: async (input) => {
    // Tool implementation
    return result;
  }
});

Scripts

  • pnpm dev - Run the CLI agent in development mode
  • pnpm api - Run the API server
  • pnpm api:dev - Run the API server in watch mode (auto-reload)
  • pnpm build - Build TypeScript to JavaScript
  • pnpm start - Run the built CLI agent

Architecture

  • src/index.ts - Main agent with CLI interface
  • src/api-server.ts - OpenAI-compatible REST API server
  • src/tools.ts - Custom MCP tools and server definition
  • src/config.ts - Configuration management
  • src/hooks.ts - Hook execution system
  • tsconfig.json - TypeScript configuration
  • .env - Environment variables (API key, port, etc.)

License

ISC

About

A custom AI agent built with the Claude Agent SDK that instantly calculates coffee costs.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages