Skip to content

Commit af9a9da

Browse files
committed
merge main and update format
2 parents 52ee285 + 112ca4e commit af9a9da

File tree

151 files changed

+2867
-17051
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

151 files changed

+2867
-17051
lines changed

.github/pull_request_template.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
1-
### PR Tips
1+
### PR Guideline
22

33
Typically, PRs should consist of a single commit, and so should generally follow
4-
the [rules for Go commit messages](https://go.dev/wiki/CommitMessage), with the following
5-
changes and additions:
4+
the [rules for Go commit messages](https://go.dev/wiki/CommitMessage).
65

7-
- Markdown is allowed.
6+
You **must** follow the form:
87

9-
- For a pervasive change, use "all" in the title instead of a package name.
8+
```
9+
net/http: handle foo when bar
10+
11+
[longer description here in the body]
12+
13+
Fixes #12345
14+
```
15+
Notably, for the subject (the first line of description):
1016

17+
- the name of the package affected by the change goes before the colon
18+
- the part after the colon uses the verb tense + phrase that completes the blank in, “this change modifies this package to ___________
19+
- the verb after the colon is lowercase
20+
- there is no trailing period
21+
- it should be kept as short as possible
22+
23+
Additionally:
24+
25+
- Markdown is allowed.
26+
- For a pervasive change, use "all" in the title instead of a package name.
1127
- The PR description should provide context (why this change?) and describe the changes
1228
at a high level. Changes that are obvious from the diffs don't need to be mentioned.

.github/workflows/test.yml

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ on:
33
# Manual trigger
44
workflow_dispatch:
55
push:
6-
branches: main
6+
branches: [main]
77
pull_request:
88

99
permissions:
@@ -13,43 +13,47 @@ jobs:
1313
lint:
1414
runs-on: ubuntu-latest
1515
steps:
16-
- name: Check out code
17-
uses: actions/checkout@v4
18-
- name: Set up Go
19-
uses: actions/setup-go@v5
20-
- name: Check formatting
21-
run: |
22-
unformatted=$(gofmt -l .)
23-
if [ -n "$unformatted" ]; then
24-
echo "The following files are not properly formatted:"
25-
echo "$unformatted"
26-
exit 1
27-
fi
28-
echo "All Go files are properly formatted"
16+
- name: Check out code
17+
uses: actions/checkout@v4
18+
- name: Set up Go
19+
uses: actions/setup-go@v5
20+
with:
21+
go-version: "^1.23"
22+
- name: Check formatting
23+
run: |
24+
unformatted=$(gofmt -l .)
25+
if [ -n "$unformatted" ]; then
26+
echo "The following files are not properly formatted:"
27+
echo "$unformatted"
28+
exit 1
29+
fi
30+
echo "All Go files are properly formatted"
31+
- name: Run Go vet
32+
run: go vet ./...
2933

3034
test:
3135
runs-on: ubuntu-latest
3236
strategy:
3337
matrix:
34-
go: [ '1.23', '1.24', '1.25.0-rc.2' ]
38+
go: ["1.23", "1.24", "1.25.0-rc.3"]
3539
steps:
36-
- name: Check out code
37-
uses: actions/checkout@v4
38-
- name: Set up Go
39-
uses: actions/setup-go@v5
40-
with:
41-
go-version: ${{ matrix.go }}
42-
- name: Test
43-
run: go test -v ./...
40+
- name: Check out code
41+
uses: actions/checkout@v4
42+
- name: Set up Go
43+
uses: actions/setup-go@v5
44+
with:
45+
go-version: ${{ matrix.go }}
46+
- name: Test
47+
run: go test -v ./...
4448

4549
race-test:
4650
runs-on: ubuntu-latest
4751
steps:
48-
- name: Check out code
49-
uses: actions/checkout@v4
50-
- name: Set up Go
51-
uses: actions/setup-go@v5
52-
with:
53-
go-version: '1.24'
54-
- name: Test with -race
55-
run: go test -v -race ./...
52+
- name: Check out code
53+
uses: actions/checkout@v4
54+
- name: Set up Go
55+
uses: actions/setup-go@v5
56+
with:
57+
go-version: "1.24"
58+
- name: Test with -race
59+
run: go test -v -race ./...

.gitignore

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Builds
2+
*.exe
3+
*.exe~
4+
dist/
5+
build/
6+
bin/
7+
*.tmp
8+
9+
# IDE files
10+
.vscode/
11+
*.code-workspace
12+
.idea/
13+
*~
14+
15+
# Go Specific
16+
*.prof
17+
*.pprof
18+
*.out
19+
*.coverage
20+
coverage.txt
21+
coverage.html
22+
23+
# OS generated files
24+
# macOS
25+
.DS_Store
26+
.DS_Store?
27+
._*
28+
.Spotlight-V100
29+
.Trashes
30+
ehthumbs.db
31+
Thumbs.db
32+
33+
# Windows
34+
Desktop.ini
35+
$RECYCLE.BIN/
36+
37+
# Linux
38+
.nfs*

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ func main() {
7373
client := mcp.NewClient(&mcp.Implementation{Name: "mcp-client", Version: "v1.0.0"}, nil)
7474

7575
// Connect to a server over stdin/stdout
76-
transport := mcp.NewCommandTransport(exec.Command("myserver"))
77-
session, err := client.Connect(ctx, transport)
76+
transport := &mcp.CommandTransport{Command: exec.Command("myserver")}
77+
session, err := client.Connect(ctx, transport, nil)
7878
if err != nil {
7979
log.Fatal(err)
8080
}
@@ -115,9 +115,9 @@ type HiParams struct {
115115
Name string `json:"name" jsonschema:"the name of the person to greet"`
116116
}
117117

118-
func SayHi(ctx context.Context, cc *mcp.ServerSession, params *mcp.CallToolParamsFor[HiParams]) (*mcp.CallToolResultFor[any], error) {
118+
func SayHi(ctx context.Context, req *mcp.ServerRequest[*mcp.CallToolParamsFor[HiParams]]) (*mcp.CallToolResultFor[any], error) {
119119
return &mcp.CallToolResultFor[any]{
120-
Content: []mcp.Content{&mcp.TextContent{Text: "Hi " + params.Arguments.Name}},
120+
Content: []mcp.Content{&mcp.TextContent{Text: "Hi " + req.Params.Arguments.Name}},
121121
}, nil
122122
}
123123

@@ -127,7 +127,7 @@ func main() {
127127

128128
mcp.AddTool(server, &mcp.Tool{Name: "greet", Description: "say hi"}, SayHi)
129129
// Run the server over stdin/stdout, until the client disconnects
130-
if err := server.Run(context.Background(), mcp.NewStdioTransport()); err != nil {
130+
if err := server.Run(context.Background(), &mcp.StdioTransport{}); err != nil {
131131
log.Fatal(err)
132132
}
133133
}

auth/auth.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
package auth
6+
7+
import (
8+
"context"
9+
"errors"
10+
"net/http"
11+
"slices"
12+
"strings"
13+
"time"
14+
)
15+
16+
type TokenInfo struct {
17+
Scopes []string
18+
Expiration time.Time
19+
}
20+
21+
type TokenVerifier func(ctx context.Context, token string) (*TokenInfo, error)
22+
23+
type RequireBearerTokenOptions struct {
24+
Scopes []string
25+
ResourceMetadataURL string
26+
}
27+
28+
var ErrInvalidToken = errors.New("invalid token")
29+
30+
type tokenInfoKey struct{}
31+
32+
// RequireBearerToken returns a piece of middleware that verifies a bearer token using the verifier.
33+
// If verification succeeds, the [TokenInfo] is added to the request's context and the request proceeds.
34+
// If verification fails, the request fails with a 401 Unauthenticated, and the WWW-Authenticate header
35+
// is populated to enable [protected resource metadata].
36+
//
37+
// [protected resource metadata]: https://datatracker.ietf.org/doc/rfc9728
38+
func RequireBearerToken(verifier TokenVerifier, opts *RequireBearerTokenOptions) func(http.Handler) http.Handler {
39+
// Based on typescript-sdk/src/server/auth/middleware/bearerAuth.ts.
40+
41+
return func(handler http.Handler) http.Handler {
42+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
43+
tokenInfo, errmsg, code := verify(r.Context(), verifier, opts, r.Header.Get("Authorization"))
44+
if code != 0 {
45+
if code == http.StatusUnauthorized || code == http.StatusForbidden {
46+
if opts != nil && opts.ResourceMetadataURL != "" {
47+
w.Header().Add("WWW-Authenticate", "Bearer resource_metadata="+opts.ResourceMetadataURL)
48+
}
49+
}
50+
http.Error(w, errmsg, code)
51+
return
52+
}
53+
r = r.WithContext(context.WithValue(r.Context(), tokenInfoKey{}, tokenInfo))
54+
handler.ServeHTTP(w, r)
55+
})
56+
}
57+
}
58+
59+
func verify(ctx context.Context, verifier TokenVerifier, opts *RequireBearerTokenOptions, authHeader string) (_ *TokenInfo, errmsg string, code int) {
60+
// Extract bearer token.
61+
fields := strings.Fields(authHeader)
62+
if len(fields) != 2 || strings.ToLower(fields[0]) != "bearer" {
63+
return nil, "no bearer token", http.StatusUnauthorized
64+
}
65+
66+
// Verify the token and get information from it.
67+
tokenInfo, err := verifier(ctx, fields[1])
68+
if err != nil {
69+
if errors.Is(err, ErrInvalidToken) {
70+
return nil, err.Error(), http.StatusUnauthorized
71+
}
72+
// TODO: the TS SDK distinguishes another error, OAuthError, and returns a 400.
73+
// Investigate how that works.
74+
// See typescript-sdk/src/server/auth/middleware/bearerAuth.ts.
75+
return nil, err.Error(), http.StatusInternalServerError
76+
}
77+
78+
// Check scopes.
79+
if opts != nil {
80+
// Note: quadratic, but N is small.
81+
for _, s := range opts.Scopes {
82+
if !slices.Contains(tokenInfo.Scopes, s) {
83+
return nil, "insufficient scope", http.StatusForbidden
84+
}
85+
}
86+
}
87+
88+
// Check expiration.
89+
if tokenInfo.Expiration.IsZero() {
90+
return nil, "token missing expiration", http.StatusUnauthorized
91+
}
92+
if tokenInfo.Expiration.Before(time.Now()) {
93+
return nil, "token expired", http.StatusUnauthorized
94+
}
95+
return tokenInfo, "", 0
96+
}

auth/auth_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
package auth
6+
7+
import (
8+
"context"
9+
"errors"
10+
"testing"
11+
"time"
12+
)
13+
14+
func TestVerify(t *testing.T) {
15+
ctx := context.Background()
16+
verifier := func(_ context.Context, token string) (*TokenInfo, error) {
17+
switch token {
18+
case "valid":
19+
return &TokenInfo{Expiration: time.Now().Add(time.Hour)}, nil
20+
case "invalid":
21+
return nil, ErrInvalidToken
22+
case "noexp":
23+
return &TokenInfo{}, nil
24+
case "expired":
25+
return &TokenInfo{Expiration: time.Now().Add(-time.Hour)}, nil
26+
default:
27+
return nil, errors.New("unknown")
28+
}
29+
}
30+
31+
for _, tt := range []struct {
32+
name string
33+
opts *RequireBearerTokenOptions
34+
header string
35+
wantMsg string
36+
wantCode int
37+
}{
38+
{
39+
"valid", nil, "Bearer valid",
40+
"", 0,
41+
},
42+
{
43+
"bad header", nil, "Barer valid",
44+
"no bearer token", 401,
45+
},
46+
{
47+
"invalid", nil, "bearer invalid",
48+
"invalid token", 401,
49+
},
50+
{
51+
"no expiration", nil, "Bearer noexp",
52+
"token missing expiration", 401,
53+
},
54+
{
55+
"expired", nil, "Bearer expired",
56+
"token expired", 401,
57+
},
58+
{
59+
"missing scope", &RequireBearerTokenOptions{Scopes: []string{"s1"}}, "Bearer valid",
60+
"insufficient scope", 403,
61+
},
62+
} {
63+
t.Run(tt.name, func(t *testing.T) {
64+
_, gotMsg, gotCode := verify(ctx, verifier, tt.opts, tt.header)
65+
if gotMsg != tt.wantMsg || gotCode != tt.wantCode {
66+
t.Errorf("got (%q, %d), want (%q, %d)", gotMsg, gotCode, tt.wantMsg, tt.wantCode)
67+
}
68+
})
69+
}
70+
}

0 commit comments

Comments
 (0)