package org.dromara.ai.mcp.tools;

import org.apache.tika.Tika;
import org.dromara.ai.mcp.McpTool;

import java.util.HashMap;
import java.util.Map;

public class FileParseTool implements McpTool {
    @Override
    public String name() {
        return "file_parse";
    }

    @Override
    public String description() {
        return "解析文件内容";
    }

    @Override
    public Map<String, Object> schema() {
        Map<String, Object> s = new HashMap<>();
        s.put("type", "object");
        s.put("properties", Map.of("path", Map.of("type", "string")));
        return s;
    }

    @Override
    public Map<String, Object> invoke(Map<String, Object> input) {
        try {
            String path = String.valueOf(input.get("path"));
            Tika tika = new Tika();
            String text = tika.parseToString(new java.io.File(path));
            return Map.of("text", text);
        } catch (Exception e) {
            return Map.of("error", e.getMessage());
        }
    }
}
