@@ -4,19 +4,20 @@ import (
44 "context"
55 "testing"
66
7- mcpmock "knative.dev/func/pkg/mcp/mock"
7+ "github.com/modelcontextprotocol/go-sdk/mcp"
8+ "knative.dev/func/pkg/mcp/mock"
89)
910
10- // TestServerCreation verifies that the MCP server can be instantiated with custom options
11- func TestServerCreation (t * testing.T ) {
11+ // TestNew ensures that the MCP server can be instantiated with custom options
12+ func TestNew (t * testing.T ) {
1213 // Define a mock executor
13- mock := mcpmock .NewExecutor ()
14- mock .ExecuteFn = func (ctx context.Context , dir string , name string , args ... string ) ([]byte , error ) {
14+ executor := mock .NewExecutor ()
15+ executor .ExecuteFn = func (ctx context.Context , dir string , name string , args ... string ) ([]byte , error ) {
1516 return []byte ("success" ), nil
1617 }
1718
1819 // Instantiate the MCP Server with mock executor and custom prefix
19- server := New (WithPrefix ("kn func" ), WithExecutor (mock ))
20+ server := New (WithPrefix ("kn func" ), WithExecutor (executor ))
2021
2122 // Verify server was created
2223 if server == nil {
@@ -28,23 +29,101 @@ func TestServerCreation(t *testing.T) {
2829 t .Errorf ("expected prefix 'kn func', got '%s'" , server .prefix )
2930 }
3031
31- // Verify executor was set correctly
32- if server .executor != mock {
32+ // Verify executor was set
33+ if server .executor != executor {
3334 t .Error ("expected executor to be the mock" )
3435 }
36+ }
37+
38+ // TestStart ensures the server starts and a client can retrieve server metadata
39+ func TestStart (t * testing.T ) {
40+ var (
41+ ctx , cancel = context .WithCancel (context .Background ())
42+ serverTpt , clientTpt = mcp .NewInMemoryTransports ()
43+ client = mcp .NewClient (& mcp.Implementation {
44+ Name : "test-client" ,
45+ Version : "1.0.0" ,
46+ }, nil )
47+ )
48+ defer cancel ()
49+
50+ // Create server with mock executor
51+ executor := mock .NewExecutor ()
52+ server := New (WithExecutor (executor ))
53+
54+ // Connect Server
55+ serverSession , err := server .Connect (ctx , serverTpt )
56+ if err != nil {
57+ t .Fatal (err )
58+ }
59+ defer serverSession .Close ()
60+
61+ // Connect Client
62+ clientSession , err := client .Connect (ctx , clientTpt , nil )
63+ if err != nil {
64+ t .Fatal (err )
65+ }
66+ defer clientSession .Close ()
67+
68+ // Verify initialization result contains server info
69+ initResult := clientSession .InitializeResult ()
70+ if initResult == nil {
71+ t .Fatal ("expected non-nil initialization result" )
72+ }
73+ if initResult .ServerInfo .Name != "func-mcp" {
74+ t .Errorf ("expected server name 'func-mcp', got %q" , initResult .ServerInfo .Name )
75+ }
76+ if initResult .ServerInfo .Version != "1.0.0" {
77+ t .Errorf ("expected server version '1.0.0', got %q" , initResult .ServerInfo .Version )
78+ }
3579
36- // Verify tools were registered
37- if len (server .tools ) != 9 {
38- t .Errorf ("expected 9 tools, got %d" , len (server .tools ))
80+ // List tools - should have all 9 registered tools
81+ toolsResult , err := clientSession .ListTools (ctx , & mcp.ListToolsParams {})
82+ if err != nil {
83+ t .Fatalf ("failed to list tools: %v" , err )
84+ }
85+ if len (toolsResult .Tools ) != 9 {
86+ t .Errorf ("expected 9 tools, got %d" , len (toolsResult .Tools ))
3987 }
4088
41- // Verify resources were registered
42- if len (server .resources ) != 13 {
43- t .Errorf ("expected 13 resources, got %d" , len (server .resources ))
89+ // Verify expected tool names are present
90+ expectedTools := map [string ]bool {
91+ "healthcheck" : false ,
92+ "create" : false ,
93+ "build" : false ,
94+ "deploy" : false ,
95+ "delete" : false ,
96+ "list" : false ,
97+ "config_volumes" : false ,
98+ "config_labels" : false ,
99+ "config_envs" : false ,
100+ }
101+ for _ , tool := range toolsResult .Tools {
102+ if _ , ok := expectedTools [tool .Name ]; ok {
103+ expectedTools [tool .Name ] = true
104+ }
105+ }
106+ for name , found := range expectedTools {
107+ if ! found {
108+ t .Errorf ("expected tool %q not found in tools list" , name )
109+ }
44110 }
45111
46- // Verify prompts were registered
47- if len (server .prompts ) != 3 {
48- t .Errorf ("expected 3 prompts, got %d" , len (server .prompts ))
112+ // List resources - should have all 13 registered resources
113+ resourcesResult , err := clientSession .ListResources (ctx , & mcp.ListResourcesParams {})
114+ if err != nil {
115+ t .Fatalf ("failed to list resources: %v" , err )
116+ }
117+ if len (resourcesResult .Resources ) != 13 {
118+ t .Errorf ("expected 13 resources, got %d" , len (resourcesResult .Resources ))
119+ }
120+
121+ // List prompts - should have all 3 registered prompts
122+ promptsResult , err := clientSession .ListPrompts (ctx , & mcp.ListPromptsParams {})
123+ if err != nil {
124+ t .Fatalf ("failed to list prompts: %v" , err )
125+ }
126+ if len (promptsResult .Prompts ) != 3 {
127+ t .Errorf ("expected 3 prompts, got %d" , len (promptsResult .Prompts ))
49128 }
50129}
0 commit comments