Skip to content

fix: fix nil pointer dereference error in error handler #109

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

Merged
merged 1 commit into from
May 22, 2025

Conversation

alexmt
Copy link
Contributor

@alexmt alexmt commented Apr 27, 2025

PR fixes the nil pointer dereference error in error response handler. The snippet below reproduces the issue:

package main

import (
	"context"
	"log"
	"os/exec"

	mcp "github.com/metoro-io/mcp-golang"
	"github.com/metoro-io/mcp-golang/transport/stdio"
)

func main() {
	cmd := exec.Command("npx", "-y", "@executeautomation/playwright-mcp-server")
	stdin, err := cmd.StdinPipe()
	if err != nil {
		log.Fatalf("Failed to get stdin pipe: %v", err)
	}
	stdout, err := cmd.StdoutPipe()
	if err != nil {
		log.Fatalf("Failed to get stdout pipe: %v", err)
	}

	if err := cmd.Start(); err != nil {
		log.Fatalf("Failed to start server: %v", err)
	}
	defer cmd.Process.Kill()

	transport := stdio.NewStdioServerTransportWithIO(stdout, stdin)
	client := mcp.NewClient(transport)

	if _, err := client.Initialize(context.Background()); err != nil {
		log.Fatalf("Failed to initialize: %v", err)
	}

	// nil value for cursor causes the error
	res, err := client.ListTools(context.Background(), nil)
	if err != nil {
		log.Fatalf("Failed to list res: %v", err)
	}
	for _, tool := range res.Tools {
		log.Printf("Tool: %s", tool.Name)
	}
}

Signed-off-by: Alexander Matyushentsev <AMatyushentsev@gmail.com>
@Copilot Copilot AI review requested due to automatic review settings April 27, 2025 22:07
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR addresses a nil pointer dereference error in the error response handler by updating how the request ID is assigned and adding a test to verify error handling.

  • Updated the handleResponse function in protocol.go to alter the assignment of the request ID.
  • Added a new test in protocol_test.go to simulate and validate error response handling.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
internal/protocol/protocol_test.go Added a test to verify that error responses are handled correctly.
internal/protocol/protocol.go Modified the request ID initialization within the error and success branches of handleResponse.

@@ -344,7 +344,7 @@ func (p *Protocol) handleCancelledNotification(notification *transport.BaseJSONR
}

func (p *Protocol) handleResponse(response *transport.BaseJSONRPCResponse, errResp *transport.BaseJSONRPCError) {
var id = response.Id
var id transport.RequestId
Copy link
Preview

Copilot AI Apr 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the error branch of handleResponse, 'id' is not initialized with response.Id, potentially leading to a zero value being used later. Consider assigning id = response.Id in both branches to ensure consistent behavior.

Copilot uses AI. Check for mistakes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

response.Id causes nil pointer dereference error

@rainu
Copy link

rainu commented May 3, 2025

I have tested this change in my application - it works fine for me 👍

BTW: This will fix the issue #96

@ecekyn ecekyn merged commit 2d128c3 into metoro-io:main May 22, 2025
1 check passed
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

Successfully merging this pull request may close these issues.

3 participants