Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return a PDF as a Tool result #162

Open
gyger opened this issue Jan 22, 2025 · 4 comments
Open

Return a PDF as a Tool result #162

gyger opened this issue Jan 22, 2025 · 4 comments

Comments

@gyger
Copy link

gyger commented Jan 22, 2025

Is your feature request related to a problem? Please describe.
I want to return a PDF instead of an Image during a Tool results using the Python SDK.

I thought the way to do it is probably like this:

@mcp.tool(description="Retrieve PDF for item in the library")
def get_item_pdf(item_key: str, attachment_index: int = 0, *, ctx: Context) -> EmbeddedResource | str:
            [......]
            with pdf_path.open('rb') as fp:
                pdf_content = fp.read()
                pdf_resource = BlobResourceContents(
                      uri=f"zotero://items/{item_key}/pdf", 
                      mimeType="application/pdf", 
                     blob=base64.b64encode(pdf_content).decode())
                return EmbeddedResource(type='resource', resource=pdf_resource)

but I only get the error

Error
too much recursion in the MCP inspector.

And Claude desktop does not like it, so I am guessing it does not work yet, or it would be great if there is an alternative version of returning things that are not images.

@Mehdi-Bl
Copy link

Mehdi-Bl commented Feb 12, 2025

Why you don't upload with the tool the PDF to remote server and provide API?
Or write it to file system and provide path?
Do you need to PDF to be processed by the AI, or only to forward it to the end user?
If you want to feed the AI, use other formats like MD or such as it will do the trick.
For the end user, as outlined don't over complicate and provide like to file system or url.

BTW: your error seem more here already related to item_key. Like you have a loop out of control.

@gyger
Copy link
Author

gyger commented Feb 12, 2025

Thanks for your input, the PDFs in that case are academic publications, and usually as a user I just upload them in the GUI. In the connector i am writing, the goal is that the PDF can just be provided from the Zotero library. Ideally I would not have to parse the paper myself into a MD format.

@Mehdi-Bl
Copy link

Mehdi-Bl commented Feb 12, 2025

I'm a bit confused here.
Did you mean to fetch MCP server side the PDF and then return it to the AI?
I have a tool that read any file and when trying to force Claude to read a PDF I got this reponse:

I apologize, but I need to clarify something important: I am not able to directly read or process PDF files. While I can use the file_read function, it's designed for text files, not PDFs. Trying to read a PDF file as text would result in garbled output since PDFs use a binary format.
To help analyze the PDF content, you would need to:

First convert the PDF to text using appropriate tools
Save the text content in a text file
Then I could read and analyze that text file

Would you like me to help you:

Find tools to convert the PDF to text, or
Look at any existing text version of this thesis if you have one?

As you can see Claude can't process binary or PDF format, so encoding it, won't solve the issue as it needs a parser on Claude API side.

Easiest way is adding https://github.com/VikParuchuri/marker in your workflow convert the file to MD ( as long not image PDF) and then enjoy, if it's an image you can use other advanced OCR converts that work fine.

Hope it helps.

@gyger
Copy link
Author

gyger commented Feb 13, 2025

I see, I guess one can provide a PDF on the Claude Desktop App, so I expected that MCP would use similar conversion tools to pretreat the document.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants