Skip to content

mcp/examples: move server example into example folder #357

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
20 changes: 2 additions & 18 deletions mcp/server_example_test.go → examples/server/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

package mcp_test
package main

import (
"context"
Expand All @@ -24,7 +24,7 @@ func SayHi(ctx context.Context, req *mcp.CallToolRequest, args SayHiParams) (*mc
}, nil, nil
}

func ExampleServer() {
func main() {
ctx := context.Background()
clientTransport, serverTransport := mcp.NewInMemoryTransports()

Expand Down Expand Up @@ -56,19 +56,3 @@ func ExampleServer() {

// Output: Hi user
}

// createSessions creates and connects an in-memory client and server session for testing purposes.
func createSessions(ctx context.Context) (*mcp.ClientSession, *mcp.ServerSession, *mcp.Server) {
server := mcp.NewServer(testImpl, nil)
client := mcp.NewClient(testImpl, nil)
serverTransport, clientTransport := mcp.NewInMemoryTransports()
serverSession, err := server.Connect(ctx, serverTransport, nil)
if err != nil {
log.Fatal(err)
}
clientSession, err := client.Connect(ctx, clientTransport, nil)
if err != nil {
log.Fatal(err)
}
return clientSession, serverSession, server
}
15 changes: 13 additions & 2 deletions mcp/client_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package mcp_test
import (
"context"
"iter"
"log"
"testing"

"github.com/google/go-cmp/cmp"
Expand All @@ -17,9 +18,19 @@ import (

func TestList(t *testing.T) {
ctx := context.Background()
clientSession, serverSession, server := createSessions(ctx)
defer clientSession.Close()
server := mcp.NewServer(testImpl, nil)
client := mcp.NewClient(testImpl, nil)
serverTransport, clientTransport := mcp.NewInMemoryTransports()
serverSession, err := server.Connect(ctx, serverTransport, nil)
if err != nil {
log.Fatal(err)
}
defer serverSession.Close()
clientSession, err := client.Connect(ctx, clientTransport, nil)
if err != nil {
log.Fatal(err)
}
defer clientSession.Close()

t.Run("tools", func(t *testing.T) {
var wantTools []*mcp.Tool
Expand Down
12 changes: 12 additions & 0 deletions mcp/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ import (

const runAsServer = "_MCP_RUN_AS_SERVER"

type SayHiParams struct {
Name string `json:"name"`
}

func SayHi(ctx context.Context, req *mcp.CallToolRequest, args SayHiParams) (*mcp.CallToolResult, any, error) {
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{Text: "Hi " + args.Name},
},
}, nil, nil
}

func TestMain(m *testing.M) {
// If the runAsServer variable is set, execute the relevant serverFunc
// instead of running tests (aka the fork and exec trick).
Expand Down