feat: initial release of torrentclaw-mcp server

MCP (Model Context Protocol) server that wraps the TorrentClaw REST API,
enabling LLMs (Claude Desktop, Claude Code, Cursor, etc.) to search movies
and TV shows with torrent downloads and streaming availability.

## Tools (6)
- search_content: primary search with filters (title, genre, year, rating,
  quality, language, sort). Returns content metadata + torrent magnet links.
- get_popular: trending content ranked by clicks
- get_recent: most recently added content
- get_watch_providers: streaming availability by country (Netflix, Disney+, etc.)
- get_credits: director and top 10 cast members
- get_torrent_url: .torrent file download URL from info_hash

## Resources
- torrentclaw://stats: catalog statistics (content/torrent counts, sources)

## Prompts (4)
- search_movie, search_show, whats_new, where_to_watch

## LLM Usability
- Server description with workflow guidance for tool chaining
- Tool descriptions include trigger phrases, cross-tool references, and
  explicit parameter examples
- Formatted output includes info_hash for tool chaining and call-syntax
  cross-references (e.g. "use with get_watch_providers(content_id=42)")
- Popular/recent output hints to use search_content for torrents
- Error messages include status-specific recovery guidance (400, 404, 429, 5xx)
- Prompts reference tools by name for reliable LLM execution

## Security
- SSRF protection: validates TORRENTCLAW_API_URL against private IPs,
  localhost, link-local, and IPv6 loopback addresses
- Protocol whitelist: only http/https allowed
- Error body sanitization: 4xx truncated to 200 chars, 5xx bodies omitted
- Input validation: control char filter on queries, regex on country codes,
  genre character whitelist, content_id bounds

## Testing
- 85 tests across 13 test files
- Coverage: 99.5% statements, 95.2% branches, 98.1% functions, 99.5% lines
- Vitest v4 with v8 coverage provider, 80% thresholds enforced

## Stack
- TypeScript ESM, Node.js >= 18 (native fetch)
- @modelcontextprotocol/sdk v1.12, zod v3.24
- STDIO transport, runnable via npx torrentclaw-mcp
This commit is contained in:
Deivid Soto 2026-02-09 17:26:23 +01:00
commit d471c9b695
36 changed files with 6000 additions and 0 deletions

142
src/formatters/content.ts Normal file
View file

@ -0,0 +1,142 @@
import type {
SearchResponse,
SearchResult,
TorrentInfo,
PopularResponse,
PopularItem,
RecentResponse,
RecentItem,
} from "../types.js";
function formatSize(bytes: string | null): string {
if (!bytes) return "?";
const b = parseInt(bytes, 10);
if (isNaN(b)) return "?";
if (b >= 1_073_741_824) return `${(b / 1_073_741_824).toFixed(1)} GB`;
if (b >= 1_048_576) return `${(b / 1_048_576).toFixed(0)} MB`;
return `${(b / 1024).toFixed(0)} KB`;
}
function formatRating(imdb: string | null, tmdb: string | null): string {
const parts: string[] = [];
if (imdb) parts.push(`IMDb: ${imdb}`);
if (tmdb) parts.push(`TMDB: ${tmdb}`);
return parts.length > 0 ? parts.join(" | ") : "No ratings";
}
function truncate(text: string, max: number): string {
if (text.length <= max) return text;
return text.slice(0, max - 3) + "...";
}
function formatTorrent(t: TorrentInfo): string {
const parts: string[] = [];
if (t.quality) parts.push(t.quality);
if (t.sourceType) parts.push(t.sourceType);
if (t.codec) parts.push(t.codec);
if (t.hdrType) parts.push(t.hdrType);
const label = parts.length > 0 ? parts.join(" ") : "Unknown quality";
const size = formatSize(t.sizeBytes);
const seeds = `${t.seeders} seeders`;
const score = t.qualityScore !== null ? `Score: ${t.qualityScore}` : null;
let line = ` - ${label} (${size}) | ${seeds}`;
if (score) line += ` | ${score}`;
line += `\n Info hash: ${t.infoHash}`;
if (t.magnetUrl) line += `\n Magnet: ${t.magnetUrl}`;
return line;
}
function formatResult(r: SearchResult, index: number): string {
const lines: string[] = [];
const yearStr = r.year ? ` (${r.year})` : "";
lines.push(`${index}. ${r.title}${yearStr} [${r.contentType}]`);
lines.push(` ${formatRating(r.ratingImdb, r.ratingTmdb)}`);
if (r.genres && r.genres.length > 0) {
lines.push(` Genres: ${r.genres.join(", ")}`);
}
if (r.overview) {
lines.push(` ${truncate(r.overview, 200)}`);
}
if (r.torrents.length > 0) {
const top = r.torrents
.sort((a, b) => (b.qualityScore ?? 0) - (a.qualityScore ?? 0))
.slice(0, 5);
lines.push(` Torrents (${r.torrents.length} total, top ${top.length}):`);
for (const t of top) {
lines.push(formatTorrent(t));
}
} else {
lines.push(" No torrents available");
}
if (r.streaming) {
const providers: string[] = [];
if (r.streaming.flatrate.length > 0)
providers.push(
`Stream: ${r.streaming.flatrate.map((p) => p.name).join(", ")}`,
);
if (r.streaming.free.length > 0)
providers.push(
`Free: ${r.streaming.free.map((p) => p.name).join(", ")}`,
);
if (providers.length > 0) lines.push(` ${providers.join(" | ")}`);
}
lines.push(
` Content ID: ${r.id} — use with get_watch_providers(content_id=${r.id}) or get_credits(content_id=${r.id})`,
);
if (r.imdbId) lines.push(` IMDb: ${r.imdbId}`);
return lines.join("\n");
}
export function formatSearchResults(data: SearchResponse): string {
if (data.results.length === 0) {
return "No results found. Try: (1) a shorter or alternate title, (2) removing filters like quality or year, (3) checking spelling. You can also try get_popular or get_recent to browse available content.";
}
const header = `Found ${data.total} results (page ${data.page}, showing ${data.results.length}):`;
const results = data.results.map((r, i) => formatResult(r, i + 1));
return [header, "", ...results].join("\n");
}
function formatPopularItem(item: PopularItem, index: number): string {
const yearStr = item.year ? ` (${item.year})` : "";
const rating = formatRating(item.ratingImdb, item.ratingTmdb);
return `${index}. ${item.title}${yearStr} [${item.contentType}] — ${rating}${item.clickCount} clicks — ID: ${item.id}`;
}
export function formatPopularResults(data: PopularResponse): string {
if (data.items.length === 0) {
return "No popular content found.";
}
const header = `Popular content (${data.total} total, page ${data.page}):`;
const hint = "(Use search_content with a title to get torrents and full details)";
const items = data.items.map((item, i) => formatPopularItem(item, i + 1));
return [header, hint, "", ...items].join("\n");
}
function formatRecentItem(item: RecentItem, index: number): string {
const yearStr = item.year ? ` (${item.year})` : "";
const rating = formatRating(item.ratingImdb, item.ratingTmdb);
const date = new Date(item.createdAt).toLocaleDateString("en-US", {
month: "short",
day: "numeric",
year: "numeric",
});
return `${index}. ${item.title}${yearStr} [${item.contentType}] — ${rating} — Added: ${date} — ID: ${item.id}`;
}
export function formatRecentResults(data: RecentResponse): string {
if (data.items.length === 0) {
return "No recent content found.";
}
const header = `Recently added content (${data.total} total, page ${data.page}):`;
const hint = "(Use search_content with a title to get torrents and full details)";
const items = data.items.map((item, i) => formatRecentItem(item, i + 1));
return [header, hint, "", ...items].join("\n");
}