Skip to content

Commit e00c485

Browse files
committed
examples: add a 'listeverything' client example, and reorganize
Add a client example that lists all features on a stdio server. Since this is our first client example, organize examples by client / server -- otherwise it is too difficult to find client examples. For #33
1 parent c132621 commit e00c485

File tree

14 files changed

+65
-0
lines changed

14 files changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2025 The Go MCP SDK Authors. All rights reserved.
2+
// Use of this source code is governed by an MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
// The listfeatures command lists all features of a stdio MCP server.
6+
//
7+
// Usage: listfeatures <command> [<args>]
8+
//
9+
// For example:
10+
//
11+
// listfeatures go run github.com/modelcontextprotocol/go-sdk/examples/server/hello
12+
//
13+
// or
14+
//
15+
// listfeatures npx @modelcontextprotocol/server-everything
16+
package main
17+
18+
import (
19+
"context"
20+
"flag"
21+
"fmt"
22+
"iter"
23+
"log"
24+
"os"
25+
"os/exec"
26+
27+
"github.com/modelcontextprotocol/go-sdk/mcp"
28+
)
29+
30+
func main() {
31+
flag.Parse()
32+
args := flag.Args()
33+
if len(args) == 0 {
34+
fmt.Fprintf(os.Stderr, "Usage: listfeatures <command> [<args>]")
35+
fmt.Fprintf(os.Stderr, "List all features for a stdio MCP server")
36+
fmt.Fprintln(os.Stderr)
37+
fmt.Fprintf(os.Stderr, "Example: listfeatures npx @modelcontextprotocol/server-everything")
38+
os.Exit(2)
39+
}
40+
41+
ctx := context.Background()
42+
cmd := exec.Command(args[0], args[1:]...)
43+
client := mcp.NewClient(&mcp.Implementation{Name: "mcp-client", Version: "v1.0.0"}, nil)
44+
cs, err := client.Connect(ctx, mcp.NewCommandTransport(cmd))
45+
if err != nil {
46+
log.Fatal(err)
47+
}
48+
defer cs.Close()
49+
50+
printSection("tools", cs.Tools(ctx, nil), func(t *mcp.Tool) string { return t.Name })
51+
printSection("resources", cs.Resources(ctx, nil), func(r *mcp.Resource) string { return r.Name })
52+
printSection("resource templates", cs.ResourceTemplates(ctx, nil), func(r *mcp.ResourceTemplate) string { return r.Name })
53+
printSection("prompts", cs.Prompts(ctx, nil), func(p *mcp.Prompt) string { return p.Name })
54+
}
55+
56+
func printSection[T any](name string, features iter.Seq2[T, error], featName func(T) string) {
57+
fmt.Printf("%s:\n", name)
58+
for feat, err := range features {
59+
if err != nil {
60+
log.Fatal(err)
61+
}
62+
fmt.Printf("\t%s\n", featName(feat))
63+
}
64+
fmt.Println()
65+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)