Skip to content

Commit 0e04158

Browse files
authored
fix: Hostname (#13)
* Change xtramcp endpoint * nit: Abstract parsing sse as helper * Fix hostname * Change protocol from https to http
1 parent 457999a commit 0e04158

File tree

4 files changed

+39
-34
lines changed

4 files changed

+39
-34
lines changed

internal/services/toolkit/client/client.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,18 @@ func NewAIClient(
4949

5050
// toolRegistry.Register("always_exception", tools.AlwaysExceptionToolDescription, tools.AlwaysExceptionTool)
5151
// toolRegistry.Register("greeting", tools.GreetingToolDescription, tools.GreetingTool)
52-
// toolRegistry.Register("paper_score", toolPaperScore.Description, toolPaperScore.Call)
53-
// toolRegistry.Register("paper_score_comment", toolPaperScoreComment.Description, toolPaperScoreComment.Call)
5452

55-
// Load tools dynamically from backend (TODO: Make URL configurable / Xtramcp url)
56-
xtraMCPLoader := xtramcp.NewXtraMCPLoader(db, projectService, "http://localhost:8080/mcp")
57-
53+
// Load tools dynamically from backend
54+
xtraMCPLoader := xtramcp.NewXtraMCPLoader(db, projectService, "http://paperdebugger-xtra-mcp-server.com/mcp")
55+
5856
// initialize MCP session first and log session ID
5957
sessionID, err := xtraMCPLoader.InitializeMCP()
6058
if err != nil {
6159
logger.Errorf("[AI Client] Failed to initialize XtraMCP session: %v", err)
6260
// TODO: Fallback to static tools or exit?
6361
} else {
6462
logger.Info("[AI Client] XtraMCP session initialized", "sessionID", sessionID)
65-
63+
6664
// dynamically load all tools from XtraMCP backend
6765
err = xtraMCPLoader.LoadToolsFromBackend(toolRegistry)
6866
if err != nil {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package xtramcp
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
// extracts JSON data from SSE format response
9+
// SSE format:
10+
//
11+
// event: message
12+
// data: { ... }
13+
func parseSSEResponse(body []byte) (string, error) {
14+
lines := strings.Split(string(body), "\n")
15+
16+
for _, line := range lines {
17+
if strings.HasPrefix(line, "data: ") {
18+
jsonData := strings.TrimPrefix(line, "data: ")
19+
return jsonData, nil
20+
}
21+
}
22+
23+
return "", fmt.Errorf("no data line found in SSE response")
24+
}

internal/services/toolkit/tools/xtramcp/loader.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"io"
88
"net/http"
9-
"strings"
109
"paperdebugger/internal/libs/db"
1110
"paperdebugger/internal/services"
1211
"paperdebugger/internal/services/toolkit/registry"
@@ -55,10 +54,10 @@ func (loader *XtraMCPLoader) LoadToolsFromBackend(toolRegistry *registry.ToolReg
5554
// Register each tool dynamically, passing the session ID
5655
for _, toolSchema := range toolSchemas {
5756
dynamicTool := NewDynamicTool(loader.db, loader.projectService, toolSchema, loader.baseURL, loader.sessionID)
58-
57+
5958
// Register the tool with the registry
6059
toolRegistry.Register(toolSchema.Name, dynamicTool.Description, dynamicTool.Call)
61-
60+
6261
fmt.Printf("Registered dynamic tool: %s\n", toolSchema.Name)
6362
}
6463

@@ -196,18 +195,9 @@ func (loader *XtraMCPLoader) fetchAvailableTools() ([]ToolSchema, error) {
196195
return nil, fmt.Errorf("failed to read response body: %w", err)
197196
}
198197

199-
// Parse SSE format - extract JSON from "data: " lines
200-
lines := strings.Split(string(bodyBytes), "\n")
201-
var extractedJSON string
202-
for _, line := range lines {
203-
if strings.HasPrefix(line, "data: ") {
204-
extractedJSON = strings.TrimPrefix(line, "data: ")
205-
break
206-
}
207-
}
208-
209-
if extractedJSON == "" {
210-
return nil, fmt.Errorf("no data line found in SSE response")
198+
extractedJSON, err := parseSSEResponse(bodyBytes)
199+
if err != nil {
200+
return nil, fmt.Errorf("failed to parse SSE response: %w", err)
211201
}
212202

213203
// Parse the extracted JSON

internal/services/toolkit/tools/xtramcp/tool.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ import (
1010
"paperdebugger/internal/libs/db"
1111
"paperdebugger/internal/services"
1212
toolCallRecordDB "paperdebugger/internal/services/toolkit/db"
13-
"strings"
1413
"time"
1514

1615
"github.com/openai/openai-go/v2"
1716
"github.com/openai/openai-go/v2/packages/param"
1817
"github.com/openai/openai-go/v2/responses"
19-
"github.com/samber/lo"
2018
)
2119

2220
// ToolSchema represents the schema from your backend
@@ -155,16 +153,11 @@ func (t *DynamicTool) executeTool(args map[string]interface{}) (string, error) {
155153
if err != nil {
156154
return "", fmt.Errorf("failed to read response: %w", err)
157155
}
158-
159-
// Parse response (assuming stream format)
160-
lines := strings.Split(string(body), "\n")
161-
lines = lo.Filter(lines, func(line string, _ int) bool {
162-
return strings.HasPrefix(line, "data:")
163-
})
164-
if len(lines) == 0 {
165-
return "", fmt.Errorf("no data line found")
156+
157+
extractedJSON, err := parseSSEResponse(body)
158+
if err != nil {
159+
return "", fmt.Errorf("failed to parse SSE response: %w", err)
166160
}
167-
line := lines[0]
168-
line = strings.TrimPrefix(line, "data: ")
169-
return line, nil
161+
162+
return extractedJSON, nil
170163
}

0 commit comments

Comments
 (0)