Skip to main content
Glama
Tavily Licensed MCP

Tavily Licensed MCP

README.md
# Tavily Licensed MCP (Copyright.sh)

[![npm version](https://badge.fury.io/js/@copyrightsh%2Ftavily-licensed-mcp.svg)](https://www.npmjs.com/package/@copyrightsh/tavily-licensed-mcp)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

**Open-source MCP server with automatic AI content licensing and token usage tracking.**

This server enables AI systems to automatically discover, respect, and log licensing for web content through Copyright.sh's license ledger. Built on Tavily's search infrastructure, it provides real-time web search, content extraction, and crawling - all with transparent license tracking for fair creator compensation.

## ๐ŸŒŸ Features

### Copyright.sh License Tracking (Core Value)
- **Automatic License Discovery**: Checks Copyright.sh `ai-license` meta tags on every URL
- **Token Usage Tracking**: Accurately counts and logs token consumption using tiktoken
- **API Key Authentication**: Authenticated usage logging for attribution and billing
- **Fair Creator Compensation**: Enables transparent royalty payments to content creators
- **Dual-Axis Licensing**: Respects stage (infer/train/tune/embed) and distribution (private/public) permissions
- **x402 Compliance**: Optional direct fetch path that handles HTTP 402 + x402 offers and retries with licensed URLs
- **Graceful Degradation**: Continues to work even if licensing API is unavailable

### Web Search & Content Capabilities (Powered by Tavily)
- **Real-time Web Search**: Comprehensive search with customizable depth and filtering
- **Content Extraction**: Extract and process content from specific URLs
- **Web Crawling**: Systematically explore websites with configurable depth/breadth
- **Site Mapping**: Create structured maps of website architecture

## ๐Ÿš€ Quick Start

### Installation

```bash
# Using npx (recommended)
npx @copyrightsh/tavily-licensed-mcp@latest

# Or install globally
npm install -g @copyrightsh/tavily-licensed-mcp
```

### Configuration

#### Claude Desktop

Add to your Claude Desktop configuration file:

**macOS**: `~/Library/Application Support/Claude/claude_desktop_config.json`
**Windows**: `%APPDATA%\Claude\claude_desktop_config.json`

```json
{
  "mcpServers": {
    "tavily-licensed": {
      "command": "npx",
      "args": ["-y", "@copyrightsh/tavily-licensed-mcp@latest"],
      "env": {
        "TAVILY_API_KEY": "tvly-your-api-key-here",
        "COPYRIGHTSH_LEDGER_API": "https://ledger.copyright.sh",
        "COPYRIGHTSH_LEDGER_API_KEY": "cs-ledger-your-key-here",
        "ENABLE_LICENSE_TRACKING": "true"
      }
    }
  }
}
```

#### VS Code / Cursor

Add to your `mcp.json`:

```json
{
  "mcpServers": {
    "tavily-licensed": {
      "command": "npx",
      "args": ["-y", "@copyrightsh/tavily-licensed-mcp@latest"],
      "env": {
        "TAVILY_API_KEY": "tvly-your-api-key-here",
        "COPYRIGHTSH_LEDGER_API": "https://ledger.copyright.sh",
        "COPYRIGHTSH_LEDGER_API_KEY": "cs-ledger-your-key-here",
        "ENABLE_LICENSE_TRACKING": "true"
      }
    }
  }
}
```

### Environment Variables

| Variable | Required | Default | Description |
|----------|----------|---------|-------------|
| `TAVILY_API_KEY` | โœ… Yes | - | Your Tavily API key ([get one here](https://tavily.com)) |
| `COPYRIGHTSH_LEDGER_API_KEY` | โš ๏ธ Recommended | - | API key for license discovery + usage logging |
| `ENABLE_LICENSE_TRACKING` | No | `true` | Enable/disable Copyright.sh integration |
| `COPYRIGHTSH_LEDGER_API` | No | Production URL | Custom ledger API endpoint |
| `LICENSE_CHECK_TIMEOUT_MS` | No | `5000` | Timeout for license discovery (ms) |
| `LICENSE_ACQUIRE_TIMEOUT_MS` | No | `8000` | Timeout for license acquisition (ms) |
| `USAGE_LOG_TIMEOUT_MS` | No | `3000` | Timeout for usage logging (ms) |
| `FETCH_TIMEOUT_MS` | No | `12000` | Timeout for direct licensed fetch (ms) |

## ๐Ÿ“š Available Tools

### `copyrightish-search`

Search the web with automatic license tracking.

```typescript
{
  query: "AI content licensing",
  search_depth: "basic" | "advanced",
  max_results: 10,
  include_domains: ["example.com"],
  exclude_domains: ["spam.com"],
  fetch: false,
  stage: "infer",
  distribution: "private",
  estimated_tokens: 1500,
  payment_method: "account_balance"
}
```

**Output includes:**
- Search results with content
- License information for each URL
- Token usage tracking
- Session summary with licensing statistics

If `fetch=true`, the server will also perform a direct licensed fetch (x402-aware) per URL and log usage from the fetched content.

### `copyrightish-extract`

Extract content from specific URLs with license checking.

```typescript
{
  urls: ["https://example.com/article1", "https://example.com/article2"],
  extract_depth: "basic" | "advanced",
  format: "markdown" | "text",
  fetch: false,
  stage: "infer",
  distribution: "private",
  estimated_tokens: 1500,
  payment_method: "account_balance"
}
```

Set `fetch=true` to bypass Tavily extraction and perform a direct licensed fetch (x402-aware).

### `copyrightish-crawl`

Crawl websites systematically with per-page license tracking.

```typescript
{
  url: "https://example.com",
  max_depth: 2,
  max_breadth: 20,
  limit: 50,
  instructions: "Find all blog posts",
  fetch: false,
  stage: "infer",
  distribution: "private",
  estimated_tokens: 1500,
  payment_method: "account_balance"
}
```

Set `fetch=true` to re-fetch crawled URLs directly with x402 support.

### `copyrightish-map`

Map website structure (lightweight license checking).

```typescript
{
  url: "https://example.com",
  max_depth: 3,
  select_paths: ["/blog/.*", "/docs/.*"]
}
```

## ๐Ÿ”’ How License Tracking Works

### 1. Content Discovery
When any tool returns web content, the server automatically:
- Extracts URLs from Tavily results
- Makes parallel requests to Copyright.sh License Ledger API
- Discovers license metadata from `<meta name="ai-license">` tags

### 2. Token Counting
- Uses `tiktoken` library for accurate GPT-4 token estimation
- Counts tokens in returned content snippets
- Applies to all text content from search/extract/crawl operations

### 3. Usage Logging
- Logs token consumption to Copyright.sh ledger
- API-key authenticated to prevent attribution fraud
- Includes: URL, token count, license version, timestamp
- Enables fair creator compensation

### 4. x402 Licensed Fetch (Optional)

When `fetch=true`, the server performs a direct fetch for each URL and:
- Detects `402 Payment Required` with `payment-required: x402`
- Calls `POST /api/v1/licenses/acquire`
- Retries with the returned `licensed_url`
- Logs usage against the acquired license

### 4. License Metadata Format

Each result includes license information:

```
Title: Example Article
URL: https://example.com/article
Content: [article text...]
๐Ÿ“œ License: Allow (private use only) | $0.15/1K tokens | Tracked โœ“

๐Ÿ“Š Session Summary:
- Total URLs: 5
- Licensed content: 3 (tracked)
- Unlicensed content: 2 (best effort)
- Total tokens: ~2,450
```

## ๐Ÿ›ก๏ธ Error Handling

The server implements graceful degradation:

- **License API unavailable**: Logs warning, continues with `license: 'unknown'`
- **Token logging fails**: Logs error but doesn't block content delivery
- **Timeout protection**: 5s for license discovery, 3s for usage logging
- **Network resilience**: Retries with exponential backoff

## ๐Ÿ—๏ธ Architecture

```
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   LLM/MCP   โ”‚
โ”‚   Client    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜
       โ”‚ query
       โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  @copyrightsh/tavily-licensed-mcp            โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚  1. Tavily API (search/crawl) โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ”‚          โ”‚ results[]               โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚  2. License Service          โ”‚  โ”‚
โ”‚  โ”‚     - Check license (async)  โ”‚โ—„โ”€โ”ผโ”€โ”€โ–บ Copyright.sh
โ”‚  โ”‚     - Estimate tokens        โ”‚  โ”‚    License Ledger API
โ”‚  โ”‚     - Log usage (async)      โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ”‚          โ”‚ enriched_results[]      โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚  3. Format with License Info โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
              โ”‚ formatted text
              โ–ผ
       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
       โ”‚   LLM sees  โ”‚
       โ”‚  licensed   โ”‚
       โ”‚   content   โ”‚
       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
```

## ๐Ÿงช Development

### Setup

```bash
git clone https://github.com/tymrtn/mcp-tavily-licensed.git
cd mcp-tavily-licensed
npm install
cp .env.example .env
# Edit .env with your API keys
```

### Build

```bash
npm run build
```

### Quick setup via CLI

```bash
# Write env template and add .mcp.json entry (defaults to project root)
node build/index.js --init

# Verify required env vars are present
node build/index.js --doctor
```

Additional flags:
- `--mcp-config <path>`: choose where to write the MCP config (defaults to `.mcp.json` in project root)
- `--env-path <path>`: choose env file location (defaults to `.env` in project root)
- `--setup-pay`: prints current payment setup steps (balance-based today)

### Testing

```bash
npm test                 # Run all tests
npm run test:watch       # Watch mode
npm run test:coverage    # Coverage report
```

### Local Development

```bash
npm run watch            # Build in watch mode
npm run inspector        # Use MCP inspector tool
```

## ๐Ÿ“– Documentation

- [Tavily API Documentation](https://docs.tavily.com)
- [Model Context Protocol](https://modelcontextprotocol.io)

## ๐Ÿค Contributing

We welcome contributions! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.

## ๐Ÿ“„ License

MIT License - see [LICENSE](LICENSE) for details.

## ๐Ÿ”— Links

- **Copyright.sh**: https://copyright.sh
- **Tavily**: https://tavily.com
- **npm package**: https://www.npmjs.com/package/@copyrightsh/tavily-licensed-mcp
- **GitHub**: https://github.com/tymrtn/mcp-tavily-licensed
- **Issues**: https://github.com/tymrtn/mcp-tavily-licensed/issues

## ๐Ÿ™ Acknowledgments

- **License Tracking**: Powered by [Copyright.sh](https://copyright.sh) license ledger
- **Search Infrastructure**: Built on Tavily's API and MCP work by Alpha AI Technologies Inc.
- **Protocol**: Implements [Model Context Protocol](https://modelcontextprotocol.io) by Anthropic

---

**Made with โค๏ธ for fair AI content licensing**