Skip to content

Commit c41f35c

Browse files
committed
chore: refactor codebase for improved structure and maintainability
- No changes to summarize; the diff is empty. Signed-off-by: appleboy <[email protected]>
1 parent e1a1a34 commit c41f35c

File tree

2 files changed

+67
-67
lines changed

2 files changed

+67
-67
lines changed

README.md

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -40,36 +40,36 @@ stdin/stdout:
4040
package main
4141

4242
import (
43-
"context"
44-
"log"
43+
"context"
44+
"log"
4545

46-
"github.com/modelcontextprotocol/go-sdk/mcp"
46+
"github.com/modelcontextprotocol/go-sdk/mcp"
4747
)
4848

4949
type Input struct {
50-
Name string `json:"name" jsonschema:"the name of the person to greet"`
50+
Name string `json:"name" jsonschema:"the name of the person to greet"`
5151
}
5252

5353
type Output struct {
54-
Greeting string `json:"greeting" jsonschema:"the greeting to tell to the user"`
54+
Greeting string `json:"greeting" jsonschema:"the greeting to tell to the user"`
5555
}
5656

5757
func SayHi(ctx context.Context, req *mcp.CallToolRequest, input Input) (
58-
*mcp.CallToolResult,
59-
Output,
60-
error,
58+
*mcp.CallToolResult,
59+
Output,
60+
error,
6161
) {
62-
return nil, Output{Greeting: "Hi " + input.Name}, nil
62+
return nil, Output{Greeting: "Hi " + input.Name}, nil
6363
}
6464

6565
func main() {
66-
// Create a server with a single tool.
67-
server := mcp.NewServer(&mcp.Implementation{Name: "greeter", Version: "v1.0.0"}, nil)
68-
mcp.AddTool(server, &mcp.Tool{Name: "greet", Description: "say hi"}, SayHi)
69-
// Run the server over stdin/stdout, until the client disconnects.
70-
if err := server.Run(context.Background(), &mcp.StdioTransport{}); err != nil {
71-
log.Fatal(err)
72-
}
66+
// Create a server with a single tool.
67+
server := mcp.NewServer(&mcp.Implementation{Name: "greeter", Version: "v1.0.0"}, nil)
68+
mcp.AddTool(server, &mcp.Tool{Name: "greet", Description: "say hi"}, SayHi)
69+
// Run the server over stdin/stdout, until the client disconnects.
70+
if err := server.Run(context.Background(), &mcp.StdioTransport{}); err != nil {
71+
log.Fatal(err)
72+
}
7373
}
7474
```
7575

@@ -81,42 +81,42 @@ stdin/stdout:
8181
package main
8282

8383
import (
84-
"context"
85-
"log"
86-
"os/exec"
84+
"context"
85+
"log"
86+
"os/exec"
8787

88-
"github.com/modelcontextprotocol/go-sdk/mcp"
88+
"github.com/modelcontextprotocol/go-sdk/mcp"
8989
)
9090

9191
func main() {
92-
ctx := context.Background()
93-
94-
// Create a new client, with no features.
95-
client := mcp.NewClient(&mcp.Implementation{Name: "mcp-client", Version: "v1.0.0"}, nil)
96-
97-
// Connect to a server over stdin/stdout.
98-
transport := &mcp.CommandTransport{Command: exec.Command("myserver")}
99-
session, err := client.Connect(ctx, transport, nil)
100-
if err != nil {
101-
log.Fatal(err)
102-
}
103-
defer session.Close()
104-
105-
// Call a tool on the server.
106-
params := &mcp.CallToolParams{
107-
Name: "greet",
108-
Arguments: map[string]any{"name": "you"},
109-
}
110-
res, err := session.CallTool(ctx, params)
111-
if err != nil {
112-
log.Fatalf("CallTool failed: %v", err)
113-
}
114-
if res.IsError {
115-
log.Fatal("tool failed")
116-
}
117-
for _, c := range res.Content {
118-
log.Print(c.(*mcp.TextContent).Text)
119-
}
92+
ctx := context.Background()
93+
94+
// Create a new client, with no features.
95+
client := mcp.NewClient(&mcp.Implementation{Name: "mcp-client", Version: "v1.0.0"}, nil)
96+
97+
// Connect to a server over stdin/stdout.
98+
transport := &mcp.CommandTransport{Command: exec.Command("myserver")}
99+
session, err := client.Connect(ctx, transport, nil)
100+
if err != nil {
101+
log.Fatal(err)
102+
}
103+
defer session.Close()
104+
105+
// Call a tool on the server.
106+
params := &mcp.CallToolParams{
107+
Name: "greet",
108+
Arguments: map[string]any{"name": "you"},
109+
}
110+
res, err := session.CallTool(ctx, params)
111+
if err != nil {
112+
log.Fatalf("CallTool failed: %v", err)
113+
}
114+
if res.IsError {
115+
log.Fatal("tool failed")
116+
}
117+
for _, c := range res.Content {
118+
log.Print(c.(*mcp.TextContent).Text)
119+
}
120120
}
121121
```
122122

examples/server/auth-middleware/README.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,12 @@ server := mcp.NewServer(&mcp.Implementation{Name: "authenticated-mcp-server"}, n
154154

155155
// Create authentication middleware
156156
authMiddleware := auth.RequireBearerToken(verifier, &auth.RequireBearerTokenOptions{
157-
Scopes: []string{"read", "write"},
157+
Scopes: []string{"read", "write"},
158158
})
159159

160160
// Create MCP handler
161161
handler := mcp.NewStreamableHTTPHandler(func(r *http.Request) *mcp.Server {
162-
return server
162+
return server
163163
}, nil)
164164

165165
// Apply authentication middleware to MCP handler
@@ -185,9 +185,9 @@ authenticatedHandler := authMiddleware(customMiddleware(handler))
185185

186186
```go
187187
func jwtVerifier(ctx context.Context, tokenString string) (*auth.TokenInfo, error) {
188-
// JWT token verification logic
189-
// On success: Return TokenInfo
190-
// On failure: Return auth.ErrInvalidToken
188+
// JWT token verification logic
189+
// On success: Return TokenInfo
190+
// On failure: Return auth.ErrInvalidToken
191191
}
192192
```
193193

@@ -196,20 +196,20 @@ func jwtVerifier(ctx context.Context, tokenString string) (*auth.TokenInfo, erro
196196
```go
197197
// Get authentication information in MCP tool
198198
func MyTool(ctx context.Context, req *mcp.CallToolRequest, args MyArgs) (*mcp.CallToolResult, any, error) {
199-
// Extract authentication info from request
200-
userInfo := req.Extra.TokenInfo
201-
202-
// Check scopes
203-
if !slices.Contains(userInfo.Scopes, "read") {
204-
return nil, nil, fmt.Errorf("insufficient permissions: read scope required")
205-
}
206-
207-
// Execute tool logic
208-
return &mcp.CallToolResult{
209-
Content: []mcp.Content{
210-
&mcp.TextContent{Text: "Tool executed successfully"},
211-
},
212-
}, nil, nil
199+
// Extract authentication info from request
200+
userInfo := req.Extra.TokenInfo
201+
202+
// Check scopes
203+
if !slices.Contains(userInfo.Scopes, "read") {
204+
return nil, nil, fmt.Errorf("insufficient permissions: read scope required")
205+
}
206+
207+
// Execute tool logic
208+
return &mcp.CallToolResult{
209+
Content: []mcp.Content{
210+
&mcp.TextContent{Text: "Tool executed successfully"},
211+
},
212+
}, nil, nil
213213
}
214214
```
215215

0 commit comments

Comments
 (0)