Skip to content

Add incoming_calls and outgoing_calls tools #9

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
Jun 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ This is an [MCP](https://modelcontextprotocol.io/introduction) server that runs
- `hover`: Display documentation, type hints, or other hover information for a given location.
- `rename_symbol`: Rename a symbol across a project.
- `edit_file`: Allows making multiple text edits to a file based on line numbers. Provides a more reliable and context-economical way to edit files compared to search and replace based edit tools.
- `callers`: Shows all locations that call a given symbol
- `callees`: Shows all functions that a given symbol calls

## About

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

---
Name: HelperFunction
Detail: github.com/isaacphi/mcp-language-server/integrationtests/test-output/go/workspace • helper.go
/TEST_OUTPUT/workspace/helper.go
Range: L4:C6 - L4:C20
- Called By: AnotherConsumer
Detail: github.com/isaacphi/mcp-language-server/integrationtests/test-output/go/workspace • another_consumer.go
/TEST_OUTPUT/workspace/another_consumer.go
Range: L6:C6 - L6:C21
- Called By: ConsumerFunction
Detail: github.com/isaacphi/mcp-language-server/integrationtests/test-output/go/workspace • consumer.go
/TEST_OUTPUT/workspace/consumer.go
Range: L6:C6 - L6:C22
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

---
Name: FooBar
Detail: github.com/isaacphi/mcp-language-server/integrationtests/test-output/go/workspace • main.go
/TEST_OUTPUT/workspace/main.go
Range: L6:C6 - L6:C12
- Called By: main
Detail: github.com/isaacphi/mcp-language-server/integrationtests/test-output/go/workspace • main.go
/TEST_OUTPUT/workspace/main.go
Range: L12:C6 - L12:C10
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

---
Name: ConsumerFunction
Detail: github.com/isaacphi/mcp-language-server/integrationtests/test-output/go/workspace • consumer.go
/TEST_OUTPUT/workspace/consumer.go
Range: L6:C6 - L6:C22
- Calls: GetName
Detail: github.com/isaacphi/mcp-language-server/integrationtests/test-output/go/workspace • types.go
/TEST_OUTPUT/workspace/types.go
Range: L21:C2 - L21:C9
- Calls: HelperFunction
Detail: github.com/isaacphi/mcp-language-server/integrationtests/test-output/go/workspace • helper.go
/TEST_OUTPUT/workspace/helper.go
Range: L4:C6 - L4:C20
- Calls: Method
Detail: github.com/isaacphi/mcp-language-server/integrationtests/test-output/go/workspace • types.go
/TEST_OUTPUT/workspace/types.go
Range: L14:C24 - L14:C30
- Calls: Println
Detail: fmt • print.go
/GOROOT/src/fmt/print.go
Range: L313:C6 - L313:C13
- Calls: Process
Detail: github.com/isaacphi/mcp-language-server/integrationtests/test-output/go/workspace • types.go
/TEST_OUTPUT/workspace/types.go
Range: L31:C24 - L31:C31
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

---
Name: main
Detail: github.com/isaacphi/mcp-language-server/integrationtests/test-output/go/workspace • main.go
/TEST_OUTPUT/workspace/main.go
Range: L12:C6 - L12:C10
- Calls: FooBar
Detail: github.com/isaacphi/mcp-language-server/integrationtests/test-output/go/workspace • main.go
/TEST_OUTPUT/workspace/main.go
Range: L6:C6 - L6:C12
- Calls: Println
Detail: fmt • print.go
/GOROOT/src/fmt/print.go
Range: L313:C6 - L313:C13
24 changes: 24 additions & 0 deletions integrationtests/tests/common/helpers.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package common

import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
Expand Down Expand Up @@ -91,10 +93,25 @@ func CleanupTestSuites(suites ...*TestSuite) {
}
}

// use instead of runtime.GOROOT which is deprecated
func getGoRoot() string {
cmd := exec.Command("go", "env", "GOROOT")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
panic(err)
}
return strings.TrimSpace(out.String())
}

// normalizePaths replaces absolute paths in the result with placeholder paths for consistent snapshots
func normalizePaths(_ *testing.T, input string) string {
// No need to get the repo root - we're just looking for patterns

// But this is useful
goroot := getGoRoot()

// Simple approach: just replace any path segments that contain workspace/
lines := strings.Split(input, "\n")
for i, line := range lines {
Expand All @@ -116,6 +133,13 @@ func normalizePaths(_ *testing.T, input string) string {
lines[i] = "/TEST_OUTPUT/workspace/" + parts[1]
}
}
if strings.Contains(line, goroot) {
parts := strings.Split(line, goroot)
if len(parts) > 1 {
// Replace with a simple placeholder path
lines[i] = "/GOROOT" + parts[1]
}
}
}

return strings.Join(lines, "\n")
Expand Down
58 changes: 58 additions & 0 deletions integrationtests/tests/go/call_hierarchy/incoming_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package callhierarchy_test

import (
"context"
"strings"
"testing"
"time"

"github.com/isaacphi/mcp-language-server/integrationtests/tests/common"
"github.com/isaacphi/mcp-language-server/integrationtests/tests/go/internal"
"github.com/isaacphi/mcp-language-server/internal/tools"
)

func TestIncomingCalls(t *testing.T) {
suite := internal.GetTestSuite(t)

ctx, cancel := context.WithTimeout(suite.Context, 10*time.Second)
defer cancel()

tests := []struct {
name string
symbolName string
expectedText string
snapshotName string
}{
{
name: "Function with calls in same file",
symbolName: "FooBar",
expectedText: ": main",
snapshotName: "incoming-same-file",
},
{
name: "Function with calls in other file",
symbolName: "HelperFunction",
expectedText: "ConsumerFunction",
snapshotName: "incoming-other-file",
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Call the GetIncomingCalls tool
result, err := tools.GetCallers(ctx, suite.Client, tc.symbolName, 1)
if err != nil {
t.Fatalf("Failed to find incoming calls: %v", err)
}

// Check that the result contains relevant information
if !strings.Contains(result, tc.expectedText) {
t.Errorf("Incoming calls do not contain expected text: %s", tc.expectedText)
}

// Use snapshot testing to verify exact output
common.SnapshotTest(t, "go", "call_hierarchy", tc.snapshotName, result)
})
}

}
58 changes: 58 additions & 0 deletions integrationtests/tests/go/call_hierarchy/outgoing_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package callhierarchy_test

import (
"context"
"strings"
"testing"
"time"

"github.com/isaacphi/mcp-language-server/integrationtests/tests/common"
"github.com/isaacphi/mcp-language-server/integrationtests/tests/go/internal"
"github.com/isaacphi/mcp-language-server/internal/tools"
)

func TestOutgoingCalls(t *testing.T) {
suite := internal.GetTestSuite(t)

ctx, cancel := context.WithTimeout(suite.Context, 10*time.Second)
defer cancel()

tests := []struct {
name string
symbolName string
expectedText string
snapshotName string
}{
{
name: "Function with calls in other file",
symbolName: "ConsumerFunction",
expectedText: "HelperFunction",
snapshotName: "outgoing-other-file",
},
{
name: "Function with calls in same file",
symbolName: "main",
expectedText: "FooBar",
snapshotName: "outgoing-same-file",
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Call the GetOutgoingCalls tool
result, err := tools.GetCallees(ctx, suite.Client, tc.symbolName, 1)
if err != nil {
t.Fatalf("Failed to find outgoing calls: %v", err)
}

// Check that the result contains relevant information
if !strings.Contains(result, tc.expectedText) {
t.Errorf("Outgoing calls do not contain expected text: %s", tc.expectedText)
}

// Use snapshot testing to verify exact output
common.SnapshotTest(t, "go", "call_hierarchy", tc.snapshotName, result)
})
}

}
Loading
Loading