mcp/examples: move server example into example folder (#357)
For better organization.
This commit is contained in:
parent
03c5113471
commit
392f719bd1
3 changed files with 27 additions and 20 deletions
58
examples/server/basic/main.go
Normal file
58
examples/server/basic/main.go
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
// Copyright 2025 The Go MCP SDK Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by an MIT-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||||
|
)
|
||||||
|
|
||||||
|
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 main() {
|
||||||
|
ctx := context.Background()
|
||||||
|
clientTransport, serverTransport := mcp.NewInMemoryTransports()
|
||||||
|
|
||||||
|
server := mcp.NewServer(&mcp.Implementation{Name: "greeter", Version: "v0.0.1"}, nil)
|
||||||
|
mcp.AddTool(server, &mcp.Tool{Name: "greet", Description: "say hi"}, SayHi)
|
||||||
|
|
||||||
|
serverSession, err := server.Connect(ctx, serverTransport, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
client := mcp.NewClient(&mcp.Implementation{Name: "client"}, nil)
|
||||||
|
clientSession, err := client.Connect(ctx, clientTransport, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := clientSession.CallTool(ctx, &mcp.CallToolParams{
|
||||||
|
Name: "greet",
|
||||||
|
Arguments: map[string]any{"name": "user"},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
fmt.Println(res.Content[0].(*mcp.TextContent).Text)
|
||||||
|
|
||||||
|
clientSession.Close()
|
||||||
|
serverSession.Wait()
|
||||||
|
|
||||||
|
// Output: Hi user
|
||||||
|
}
|
||||||
|
|
@ -7,6 +7,7 @@ package mcp_test
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"iter"
|
"iter"
|
||||||
|
"log"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
|
|
@ -17,9 +18,19 @@ import (
|
||||||
|
|
||||||
func TestList(t *testing.T) {
|
func TestList(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
clientSession, serverSession, server := createSessions(ctx)
|
server := mcp.NewServer(testImpl, nil)
|
||||||
defer clientSession.Close()
|
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()
|
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) {
|
t.Run("tools", func(t *testing.T) {
|
||||||
var wantTools []*mcp.Tool
|
var wantTools []*mcp.Tool
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,18 @@ import (
|
||||||
|
|
||||||
const runAsServer = "_MCP_RUN_AS_SERVER"
|
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) {
|
func TestMain(m *testing.M) {
|
||||||
// If the runAsServer variable is set, execute the relevant serverFunc
|
// If the runAsServer variable is set, execute the relevant serverFunc
|
||||||
// instead of running tests (aka the fork and exec trick).
|
// instead of running tests (aka the fork and exec trick).
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue