mcp-go icon indicating copy to clipboard operation
mcp-go copied to clipboard

PDF content

Open loeffel-io opened this issue 8 months ago • 4 comments

Hey, how to add a pdf content? tried a lot of stuff like

		callToolResult.Content = append(callToolResult.Content, mcp.ImageContent{
			Type:     "pdf",
			Data:     base64.StdEncoding.EncodeToString(pdfBytes),
			MIMEType: "application/pdf",
		})

loeffel-io avatar Mar 19 '25 13:03 loeffel-io

also this

		callToolResult.Content = append(callToolResult.Content, mcp.EmbeddedResource{
			Type: "resource",
			Resource: mcp.BlobResourceContents{
				URI:      "file://" + pdfPath,
				MIMEType: "application/pdf",
				Blob:     base64.StdEncoding.EncodeToString(pdfBytes),
			},
		})

responds with Unsupported image type: application/pdf

loeffel-io avatar Mar 19 '25 13:03 loeffel-io

my mcp inspector prints this

{
  "uri": "file:///Users/loeffel/Google Drive/My Drive/Gewerbe/Steuer/Steuer 2025/Februar/N26/google.com/6382285305241639-10.pdf",
  "mimeType": "application/pdf",
  "blob": "{base64content}"
}

i can open the uri in my browser do i need to add s.AddResources or something - i am stuck 😄

its a simple tool call

	s.AddTool(n26ToolGet, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
		id, err := uuid.Parse(request.Params.Arguments["id"].(string))
		if err != nil {
			return mcp.NewToolResultError("Invalid id format"), nil
		}

		transaction, ok := transactions[id.String()]
		if !ok {
			return mcp.NewToolResultError("Transaction not found"), nil
		}

		result := &ResultGet{
			Id:          id,
			Transaction: transaction,
		}

		resultBytes, err := json.Marshal(result)
		if err != nil {
			return mcp.NewToolResultError("Error marshalling transactions"), nil
		}

		callToolResult := &mcp.CallToolResult{
			Content: []mcp.Content{
				mcp.TextContent{
					Type: "text",
					Text: "Full transaction",
				},
				mcp.NewEmbeddedResource(
					mcp.TextResourceContents{
						MIMEType: "application/json",
						Text:     string(resultBytes),
					},
				),
			},
			IsError: false,
		}

		pdfPath, ok := strings.CutPrefix(transaction.Info, "output/")
		if !ok {
			return callToolResult, nil
		}

		pdfPath = dir + "/" + pdfPath
		_, err = os.Stat(pdfPath)
		if errors.Is(err, os.ErrNotExist) {
			return callToolResult, nil
		}

		pdfBytes, err := os.ReadFile(pdfPath)
		if err != nil {
			return mcp.NewToolResultError("Error reading pdf file"), nil
		}

		log.Printf("pdfPath: %s", "file://"+pdfPath)

		callToolResult.Content = append(callToolResult.Content, mcp.NewEmbeddedResource(
			mcp.BlobResourceContents{
				URI:      "file://" + pdfPath,
				MIMEType: "application/pdf",
				Blob:     base64.StdEncoding.EncodeToString(pdfBytes),
			},
		))

		return callToolResult, nil
	})

loeffel-io avatar Mar 19 '25 14:03 loeffel-io

Claude should be also has access to the files

Image

loeffel-io avatar Mar 19 '25 14:03 loeffel-io

I'm on Linux and don't have access to Claude Desktop, anyone else having issues with this and willing to debug?

ezynda3 avatar Mar 21 '25 13:03 ezynda3