Add PDF text extraction to MCP read_file and search_files

MCP's read_file previously hard-required UTF-8 text decoding, so any
PDF in an allowed folder failed with "Cannot read file as UTF-8
text" — chat attachments already supported PDFs (raw bytes to
vision-capable models), but the AI couldn't read one on its own
during agentic file-tool use. search_files' content_search had the
same gap, silently skipping PDFs.

Adds MCPService.extractPDFText(atPath:) using PDFKit (built into
macOS, no new dependency) to pull text from a PDF's text layer.
Wired into both read_file and search_files' content search. Returns
a clear error for scanned/image-only PDFs with no text layer.
Automatically covers the Research Agents sub-agent tool loop too,
since it shares the same executeTool dispatcher.

Verified live: asked the AI to read a real PDF containing "The
secret code is PINEAPPLE-42." via read_file — it extracted the text
correctly through the MCP tool-call path.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-25 11:44:27 +02:00
co-authored by Claude Sonnet 5
parent 377e783a17
commit 69dbf17f5e
2 changed files with 102 additions and 8 deletions
+34 -8
View File
@@ -23,6 +23,7 @@
import Foundation
import os
import PDFKit
@Observable
class MCPService {
@@ -155,7 +156,7 @@ class MCPService {
var tools: [Tool] = [
makeTool(
name: "read_file",
description: "Read the contents of a file. Returns the text content of the file. Maximum file size is 10MB.",
description: "Read the contents of a file. Returns the text content of the file. PDFs are supported (text layer is extracted automatically) — scanned/image-only PDFs with no text layer will return an error. Maximum file size is 10MB.",
properties: [
"file_path": prop("string", "The absolute path to the file to read")
],
@@ -172,7 +173,7 @@ class MCPService {
),
makeTool(
name: "search_files",
description: "Search for files by name pattern or content. Use 'pattern' for filename glob matching (e.g. '*.swift'). Use 'content_search' for searching inside file contents.",
description: "Search for files by name pattern or content. Use 'pattern' for filename glob matching (e.g. '*.swift'). Use 'content_search' for searching inside file contents (PDF text layers are searched too).",
properties: [
"pattern": prop("string", "Glob pattern to match filenames (e.g. '*.py', 'README*')"),
"search_path": prop("string", "Directory to search in (defaults to first allowed folder)"),
@@ -536,8 +537,17 @@ class MCPService {
return ["error": "File too large (\(sizeMB) MB, max 10 MB)"]
}
guard let content = try? String(contentsOfFile: resolved, encoding: .utf8) else {
return ["error": "Cannot read file as UTF-8 text: \(filePath)"]
let content: String
if (resolved as NSString).pathExtension.lowercased() == "pdf" {
guard let extracted = extractPDFText(atPath: resolved) else {
return ["error": "Could not extract text from PDF (it may be scanned/image-only with no text layer, encrypted, or corrupted): \(filePath)"]
}
content = extracted
} else {
guard let text = try? String(contentsOfFile: resolved, encoding: .utf8) else {
return ["error": "Cannot read file as UTF-8 text: \(filePath)"]
}
content = text
}
var finalContent = content
@@ -554,6 +564,19 @@ class MCPService {
return ["content": finalContent, "path": resolved, "size": fileSize]
}
/// Extracts plain text from a PDF's text layer, page by page. Returns nil for
/// scanned/image-only PDFs (no text layer), encrypted, or otherwise unreadable files.
func extractPDFText(atPath path: String) -> String? {
guard let document = PDFDocument(url: URL(fileURLWithPath: path)) else { return nil }
var text = ""
for i in 0..<document.pageCount {
guard let page = document.page(at: i), let pageText = page.string else { continue }
text += pageText + "\n\n"
}
let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines)
return trimmed.isEmpty ? nil : text
}
private func listDirectory(dirPath: String, recursive: Bool) -> [String: Any] {
let resolved = ((dirPath as NSString).expandingTildeInPath as NSString).standardizingPath
@@ -660,10 +683,13 @@ class MCPService {
// Content search if requested
if let searchText = contentSearch, !searchText.isEmpty {
guard let content = try? String(contentsOfFile: fullPath, encoding: .utf8) else {
continue
let content: String?
if (fullPath as NSString).pathExtension.lowercased() == "pdf" {
content = extractPDFText(atPath: fullPath)
} else {
content = try? String(contentsOfFile: fullPath, encoding: .utf8)
}
if !content.localizedCaseInsensitiveContains(searchText) {
guard let content, content.localizedCaseInsensitiveContains(searchText) else {
continue
}
}
@@ -944,7 +970,7 @@ class MCPService {
let readOnlyTools: [Tool] = [
makeTool(
name: "read_file",
description: "Read the contents of a file. Maximum file size is 10MB.",
description: "Read the contents of a file. PDFs are supported (text layer extracted automatically). Maximum file size is 10MB.",
properties: ["file_path": prop("string", "The absolute path to the file to read")],
required: ["file_path"]
),