Skip to content

Commit 3a0b9d3

Browse files
Mock MCP client for testing
1 parent 1f0dc74 commit 3a0b9d3

File tree

4 files changed

+325
-24
lines changed

4 files changed

+325
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Add refresh title feature to refresh chat title from LLM
1313
- Implement MCP Prompts functionality
14+
- Add `MCPClient` interface for testing purposes
1415

1516
### Fixed
1617

cmd/server/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func main() {
5757
}
5858

5959
mcpClients, stdIOCmds := populateMCPClients(cfg, mcpClientInfo, logger)
60+
mcpClis := make([]handlers.MCPClient, len(mcpClients))
6061

6162
for i, cli := range mcpClients {
6263
logger.Info("Connecting to MCP server", slog.Int("index", i))
@@ -70,12 +71,12 @@ func main() {
7071
}
7172
connectCancel()
7273

73-
mcpClients[i] = cli
74+
mcpClis[i] = cli
7475

7576
logger.Info("Connected to MCP server", slog.String("name", mcpClients[i].ServerInfo().Name))
7677
}
7778

78-
m, err := handlers.NewMain(llm, titleGen, boltDB, mcpClients, logger)
79+
m, err := handlers.NewMain(llm, titleGen, boltDB, mcpClis, logger)
7980
if err != nil {
8081
panic(err)
8182
}

internal/handlers/main.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ type Store interface {
3838
UpdateMessage(ctx context.Context, chatID string, message models.Message) error
3939
}
4040

41+
// MCPClient defines the interface for interacting with an MCP server.
42+
// This allows for mocking in tests.
43+
type MCPClient interface {
44+
ServerInfo() mcp.Info
45+
ToolServerSupported() bool
46+
ResourceServerSupported() bool
47+
PromptServerSupported() bool
48+
ListTools(ctx context.Context, params mcp.ListToolsParams) (mcp.ListToolsResult, error)
49+
ListResources(ctx context.Context, params mcp.ListResourcesParams) (mcp.ListResourcesResult, error)
50+
ListPrompts(ctx context.Context, params mcp.ListPromptsParams) (mcp.ListPromptResult, error)
51+
GetPrompt(ctx context.Context, params mcp.GetPromptParams) (mcp.GetPromptResult, error)
52+
CallTool(ctx context.Context, params mcp.CallToolParams) (mcp.CallToolResult, error)
53+
}
54+
4155
// Main handles the core functionality of the chat application, managing server-sent events,
4256
// HTML templates, and interactions between the LLM and Store components.
4357
type Main struct {
@@ -48,7 +62,7 @@ type Main struct {
4862
titleGenerator TitleGenerator
4963
store Store
5064

51-
mcpClients []*mcp.Client
65+
mcpClients []MCPClient
5266

5367
servers []mcp.Info
5468
tools []mcp.Tool
@@ -73,7 +87,7 @@ func NewMain(
7387
llm LLM,
7488
titleGen TitleGenerator,
7589
store Store,
76-
mcpClients []*mcp.Client,
90+
mcpClients []MCPClient,
7791
logger *slog.Logger,
7892
) (Main, error) {
7993
// We parse templates from three distinct directories to separate layout, pages, and partial views

0 commit comments

Comments
 (0)