Skip to content

Commit 85de8b0

Browse files
committed
merge with main
2 parents bb33f9e + be0a00c commit 85de8b0

File tree

167 files changed

+6479
-18301
lines changed

Some content is hidden

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

167 files changed

+6479
-18301
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: 39 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,51 @@ 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 ./...
33+
- name: Run staticcheck
34+
uses: dominikh/staticcheck-action@v1
35+
with:
36+
version: "latest"
2937

3038
test:
3139
runs-on: ubuntu-latest
3240
strategy:
3341
matrix:
34-
go: [ '1.23', '1.24', '1.25.0-rc.2' ]
42+
go: ["1.23", "1.24", "1.25.0-rc.3"]
3543
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 ./...
44+
- name: Check out code
45+
uses: actions/checkout@v4
46+
- name: Set up Go
47+
uses: actions/setup-go@v5
48+
with:
49+
go-version: ${{ matrix.go }}
50+
- name: Test
51+
run: go test -v ./...
4452

4553
race-test:
4654
runs-on: ubuntu-latest
4755
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 ./...
56+
- name: Check out code
57+
uses: actions/checkout@v4
58+
- name: Set up Go
59+
uses: actions/setup-go@v5
60+
with:
61+
go-version: "1.24"
62+
- name: Test with -race
63+
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: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
<!-- Autogenerated by weave; DO NOT EDIT -->
2-
# MCP Go SDK v0.2.0
2+
# MCP Go SDK v0.3.0
33

44
[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/modelcontextprotocol/go-sdk)
55

66
***BREAKING CHANGES***
77

88
This version contains breaking changes.
99
See the [release notes](
10-
https://github.com/modelcontextprotocol/go-sdk/releases/tag/v0.2.0) for details.
10+
https://github.com/modelcontextprotocol/go-sdk/releases/tag/v0.3.0) for details.
1111

1212
[![PkgGoDev](https://pkg.go.dev/badge/github.com/modelcontextprotocol/go-sdk)](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk)
1313

1414
This repository contains an unreleased implementation of the official Go
1515
software development kit (SDK) for the Model Context Protocol (MCP).
1616

1717
> [!WARNING]
18-
> The SDK should be considered unreleased, and is currently unstable
19-
> and subject to breaking changes. Please test it out and file bug reports or API
20-
> proposals, but don't use it in real projects. See the issue tracker for known
21-
> issues and missing features. We aim to release a stable version of the SDK in
22-
> August, 2025.
18+
> The SDK is not yet at v1.0.0 and may still be subject to incompatible API
19+
> changes. We aim to tag v1.0.0 in September, 2025. See
20+
> https://github.com/modelcontextprotocol/go-sdk/issues/328 for details.
2321
2422
## Design
2523

@@ -73,8 +71,8 @@ func main() {
7371
client := mcp.NewClient(&mcp.Implementation{Name: "mcp-client", Version: "v1.0.0"}, nil)
7472

7573
// Connect to a server over stdin/stdout
76-
transport := mcp.NewCommandTransport(exec.Command("myserver"))
77-
session, err := client.Connect(ctx, transport)
74+
transport := &mcp.CommandTransport{Command: exec.Command("myserver")}
75+
session, err := client.Connect(ctx, transport, nil)
7876
if err != nil {
7977
log.Fatal(err)
8078
}
@@ -115,10 +113,10 @@ type HiParams struct {
115113
Name string `json:"name" jsonschema:"the name of the person to greet"`
116114
}
117115

118-
func SayHi(ctx context.Context, cc *mcp.ServerSession, params *mcp.CallToolParamsFor[HiParams]) (*mcp.CallToolResultFor[any], error) {
119-
return &mcp.CallToolResultFor[any]{
120-
Content: []mcp.Content{&mcp.TextContent{Text: "Hi " + params.Arguments.Name}},
121-
}, nil
116+
func SayHi(ctx context.Context, req *mcp.CallToolRequest, args HiParams) (*mcp.CallToolResult, any, error) {
117+
return &mcp.CallToolResult{
118+
Content: []mcp.Content{&mcp.TextContent{Text: "Hi " + args.Name}},
119+
}, nil, nil
122120
}
123121

124122
func main() {
@@ -127,7 +125,7 @@ func main() {
127125

128126
mcp.AddTool(server, &mcp.Tool{Name: "greet", Description: "say hi"}, SayHi)
129127
// Run the server over stdin/stdout, until the client disconnects
130-
if err := server.Run(context.Background(), mcp.NewStdioTransport()); err != nil {
128+
if err := server.Run(context.Background(), &mcp.StdioTransport{}); err != nil {
131129
log.Fatal(err)
132130
}
133131
}

auth/auth.go

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
// TokenInfo holds information from a bearer token.
17+
type TokenInfo struct {
18+
Scopes []string
19+
Expiration time.Time
20+
// TODO: add standard JWT fields
21+
Extra map[string]any
22+
}
23+
24+
// The error that a TokenVerifier should return if the token cannot be verified.
25+
var ErrInvalidToken = errors.New("invalid token")
26+
27+
// A TokenVerifier checks the validity of a bearer token, and extracts information
28+
// from it. If verification fails, it should return an error that unwraps to ErrInvalidToken.
29+
type TokenVerifier func(ctx context.Context, token string) (*TokenInfo, error)
30+
31+
// RequireBearerTokenOptions are options for [RequireBearerToken].
32+
type RequireBearerTokenOptions struct {
33+
// The URL for the resource server metadata OAuth flow, to be returned as part
34+
// of the WWW-Authenticate header.
35+
ResourceMetadataURL string
36+
// The required scopes.
37+
Scopes []string
38+
}
39+
40+
type tokenInfoKey struct{}
41+
42+
// TokenInfoFromContext returns the [TokenInfo] stored in ctx, or nil if none.
43+
func TokenInfoFromContext(ctx context.Context) *TokenInfo {
44+
ti := ctx.Value(tokenInfoKey{})
45+
if ti == nil {
46+
return nil
47+
}
48+
return ti.(*TokenInfo)
49+
}
50+
51+
// RequireBearerToken returns a piece of middleware that verifies a bearer token using the verifier.
52+
// If verification succeeds, the [TokenInfo] is added to the request's context and the request proceeds.
53+
// If verification fails, the request fails with a 401 Unauthenticated, and the WWW-Authenticate header
54+
// is populated to enable [protected resource metadata].
55+
//
56+
// [protected resource metadata]: https://datatracker.ietf.org/doc/rfc9728
57+
func RequireBearerToken(verifier TokenVerifier, opts *RequireBearerTokenOptions) func(http.Handler) http.Handler {
58+
// Based on typescript-sdk/src/server/auth/middleware/bearerAuth.ts.
59+
60+
return func(handler http.Handler) http.Handler {
61+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
62+
tokenInfo, errmsg, code := verify(r.Context(), verifier, opts, r.Header.Get("Authorization"))
63+
if code != 0 {
64+
if code == http.StatusUnauthorized || code == http.StatusForbidden {
65+
if opts != nil && opts.ResourceMetadataURL != "" {
66+
w.Header().Add("WWW-Authenticate", "Bearer resource_metadata="+opts.ResourceMetadataURL)
67+
}
68+
}
69+
http.Error(w, errmsg, code)
70+
return
71+
}
72+
r = r.WithContext(context.WithValue(r.Context(), tokenInfoKey{}, tokenInfo))
73+
handler.ServeHTTP(w, r)
74+
})
75+
}
76+
}
77+
78+
func verify(ctx context.Context, verifier TokenVerifier, opts *RequireBearerTokenOptions, authHeader string) (_ *TokenInfo, errmsg string, code int) {
79+
// Extract bearer token.
80+
fields := strings.Fields(authHeader)
81+
if len(fields) != 2 || strings.ToLower(fields[0]) != "bearer" {
82+
return nil, "no bearer token", http.StatusUnauthorized
83+
}
84+
85+
// Verify the token and get information from it.
86+
tokenInfo, err := verifier(ctx, fields[1])
87+
if err != nil {
88+
if errors.Is(err, ErrInvalidToken) {
89+
return nil, err.Error(), http.StatusUnauthorized
90+
}
91+
// TODO: the TS SDK distinguishes another error, OAuthError, and returns a 400.
92+
// Investigate how that works.
93+
// See typescript-sdk/src/server/auth/middleware/bearerAuth.ts.
94+
return nil, err.Error(), http.StatusInternalServerError
95+
}
96+
97+
// Check scopes. All must be present.
98+
if opts != nil {
99+
// Note: quadratic, but N is small.
100+
for _, s := range opts.Scopes {
101+
if !slices.Contains(tokenInfo.Scopes, s) {
102+
return nil, "insufficient scope", http.StatusForbidden
103+
}
104+
}
105+
}
106+
107+
// Check expiration.
108+
if tokenInfo.Expiration.IsZero() {
109+
return nil, "token missing expiration", http.StatusUnauthorized
110+
}
111+
if tokenInfo.Expiration.Before(time.Now()) {
112+
return nil, "token expired", http.StatusUnauthorized
113+
}
114+
return tokenInfo, "", 0
115+
}

0 commit comments

Comments
 (0)