Skip to content

Commit b05d0f9

Browse files
committed
refactor: refactor codebase for improved formatting and readability
- Improve formatting by breaking up long function argument lists onto multiple lines for clarity and consistency - Update test error reporting to use multi-line formatting for better readability - No functional changes to logic or behavior; solely refactoring for style and maintainability Signed-off-by: appleboy <appleboy.tw@gmail.com>
1 parent e3d1d7f commit b05d0f9

File tree

7 files changed

+90
-23
lines changed

7 files changed

+90
-23
lines changed

02-basic-token-passthrough/server.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,13 @@ func (s *MCPServer) ServeHTTP() *server.StreamableHTTPServer {
6767
// ServeStdio starts the MCP server using stdio transport, injecting the
6868
// auth token from the environment into the context.
6969
func (s *MCPServer) ServeStdio() error {
70-
return server.ServeStdio(s.server, server.WithStdioContextFunc(func(ctx context.Context) context.Context {
71-
ctx = core.AuthFromEnv(ctx)
72-
return core.WithRequestID(ctx)
73-
}))
70+
return server.ServeStdio(
71+
s.server,
72+
server.WithStdioContextFunc(func(ctx context.Context) context.Context {
73+
ctx = core.AuthFromEnv(ctx)
74+
return core.WithRequestID(ctx)
75+
}),
76+
)
7477
}
7578

7679
func main() {

04-observability/server.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,12 @@ func main() {
100100
var addr string
101101
flag.StringVar(&addr, "addr", ":8080", "address to listen on")
102102
// Support both short (-t) and long (--transport) flags for convenience.
103-
flag.StringVar(&transport, "t", "stdio", "Transport type (stdio or http) (alias for --transport)")
103+
flag.StringVar(
104+
&transport,
105+
"t",
106+
"stdio",
107+
"Transport type (stdio or http) (alias for --transport)",
108+
)
104109
flag.StringVar(&transport, "transport", "stdio", "Transport type (stdio or http)")
105110
flag.Parse()
106111

pkg/operation/echo/echo.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ import (
77
"github.com/mark3labs/mcp-go/mcp"
88
)
99

10-
var EchoMessageTool = mcp.NewTool("echo_message",
11-
mcp.WithDescription("Returns the input message prefixed with 'Echo: '. Useful for testing integration and server responsiveness."), // Tool description
10+
var EchoMessageTool = mcp.NewTool(
11+
"echo_message",
12+
mcp.WithDescription(
13+
"Returns the input message prefixed with 'Echo: '. Useful for testing integration and server responsiveness.",
14+
), // Tool description
1215
mcp.WithString("message", // Tool argument name
1316
mcp.Description("The message to echo back in the response."), // Argument description
1417
mcp.Required(), // Argument is required
@@ -23,7 +26,10 @@ var EchoMessageTool = mcp.NewTool("echo_message",
2326
// Returns:
2427
// - *mcp.CallToolResult: the result of the tool execution (here, a text message)
2528
// - error: returns an error if the argument is invalid or processing fails
26-
func HandleEchoMessageTool(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
29+
func HandleEchoMessageTool(
30+
ctx context.Context,
31+
req mcp.CallToolRequest,
32+
) (*mcp.CallToolResult, error) {
2733
// Retrieve the "message" argument from arguments and check its type
2834
message, ok := req.GetArguments()["message"].(string)
2935
if !ok {

pkg/store/memory.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ func NewMemoryStore() *MemoryStore {
4242

4343
// SaveAuthorizationCode stores an authorization code in memory.
4444
// It returns an error if the code is nil or the code string is empty.
45-
func (m *MemoryStore) SaveAuthorizationCode(ctx context.Context, code *core.AuthorizationCode) error {
45+
func (m *MemoryStore) SaveAuthorizationCode(
46+
ctx context.Context,
47+
code *core.AuthorizationCode,
48+
) error {
4649
if code == nil {
4750
return ErrNilAuthorizationCode
4851
}
@@ -60,7 +63,10 @@ func (m *MemoryStore) SaveAuthorizationCode(ctx context.Context, code *core.Auth
6063
// GetAuthorizationCode retrieves an authorization code from memory by its code string.
6164
// It returns ErrCodeNotFound if the code does not exist.
6265
// If the code has expired, it will be automatically deleted and ErrCodeNotFound is returned.
63-
func (m *MemoryStore) GetAuthorizationCode(ctx context.Context, clientID string) (*core.AuthorizationCode, error) {
66+
func (m *MemoryStore) GetAuthorizationCode(
67+
ctx context.Context,
68+
clientID string,
69+
) (*core.AuthorizationCode, error) {
6470
if clientID == "" {
6571
return nil, ErrEmptyClientID
6672
}

pkg/store/memory_test.go

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ func TestMemoryStore_SaveAuthorizationCode(t *testing.T) {
9191
t.Errorf("Failed to retrieve saved code: %v", getErr)
9292
}
9393
if savedCode.Code != tt.code.Code {
94-
t.Errorf("Retrieved code mismatch: got %v, want %v", savedCode.Code, tt.code.Code)
94+
t.Errorf(
95+
"Retrieved code mismatch: got %v, want %v",
96+
savedCode.Code,
97+
tt.code.Code,
98+
)
9599
}
96100
}
97101
})
@@ -182,8 +186,13 @@ func TestMemoryStore_GetAuthorizationCode(t *testing.T) {
182186
t.Errorf("Expected no authorization code, but got %v", gotCode)
183187
}
184188

185-
if tt.wantCode && gotCode != nil && tt.setupCode != nil && gotCode.Code != tt.setupCode.Code {
186-
t.Errorf("GetAuthorizationCode() code = %v, want %v", gotCode.Code, tt.setupCode.Code)
189+
if tt.wantCode && gotCode != nil && tt.setupCode != nil &&
190+
gotCode.Code != tt.setupCode.Code {
191+
t.Errorf(
192+
"GetAuthorizationCode() code = %v, want %v",
193+
gotCode.Code,
194+
tt.setupCode.Code,
195+
)
187196
}
188197
})
189198
}
@@ -267,11 +276,19 @@ func TestMemoryStore_UpdateExistingCode(t *testing.T) {
267276
}
268277

269278
if retrievedCode.Code != "updated_code_456" {
270-
t.Errorf("Code was not updated. Got Code %v, want %v", retrievedCode.Code, "updated_code_456")
279+
t.Errorf(
280+
"Code was not updated. Got Code %v, want %v",
281+
retrievedCode.Code,
282+
"updated_code_456",
283+
)
271284
}
272285

273286
if retrievedCode.RedirectURI != "https://updated.com/callback" {
274-
t.Errorf("RedirectURI was not updated. Got %v, want %v", retrievedCode.RedirectURI, "https://updated.com/callback")
287+
t.Errorf(
288+
"RedirectURI was not updated. Got %v, want %v",
289+
retrievedCode.RedirectURI,
290+
"https://updated.com/callback",
291+
)
275292
}
276293

277294
if len(retrievedCode.Scope) != 2 {
@@ -458,7 +475,11 @@ func TestMemoryStore_GetClient(t *testing.T) {
458475
t.Errorf("GetClient() ID = %v, want %v", gotClient.ID, tt.setupClient.ID)
459476
}
460477
if gotClient.Secret != tt.setupClient.Secret {
461-
t.Errorf("GetClient() Secret = %v, want %v", gotClient.Secret, tt.setupClient.Secret)
478+
t.Errorf(
479+
"GetClient() Secret = %v, want %v",
480+
gotClient.Secret,
481+
tt.setupClient.Secret,
482+
)
462483
}
463484
}
464485
})
@@ -518,7 +539,11 @@ func TestMemoryStore_CreateClient(t *testing.T) {
518539
t.Errorf("Failed to retrieve created client: %v", getErr)
519540
}
520541
if gotClient.ID != tt.client.ID {
521-
t.Errorf("Retrieved client ID mismatch: got %v, want %v", gotClient.ID, tt.client.ID)
542+
t.Errorf(
543+
"Retrieved client ID mismatch: got %v, want %v",
544+
gotClient.ID,
545+
tt.client.ID,
546+
)
522547
}
523548
}
524549
})
@@ -601,10 +626,18 @@ func TestMemoryStore_UpdateClient(t *testing.T) {
601626
t.Errorf("Failed to retrieve updated client: %v", getErr)
602627
}
603628
if gotClient.Secret != tt.updateClient.Secret {
604-
t.Errorf("Client secret was not updated. Got %v, want %v", gotClient.Secret, tt.updateClient.Secret)
629+
t.Errorf(
630+
"Client secret was not updated. Got %v, want %v",
631+
gotClient.Secret,
632+
tt.updateClient.Secret,
633+
)
605634
}
606635
if gotClient.Scope != tt.updateClient.Scope {
607-
t.Errorf("Client scope was not updated. Got %v, want %v", gotClient.Scope, tt.updateClient.Scope)
636+
t.Errorf(
637+
"Client scope was not updated. Got %v, want %v",
638+
gotClient.Scope,
639+
tt.updateClient.Scope,
640+
)
608641
}
609642
}
610643
})

pkg/store/redis.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ func (r *RedisStore) Close() {
6767

6868
// SaveAuthorizationCode stores an authorization code in Redis with TTL.
6969
// It returns an error if the code is nil or the client ID is empty.
70-
func (r *RedisStore) SaveAuthorizationCode(ctx context.Context, code *core.AuthorizationCode) error {
70+
func (r *RedisStore) SaveAuthorizationCode(
71+
ctx context.Context,
72+
code *core.AuthorizationCode,
73+
) error {
7174
if code == nil {
7275
return ErrNilAuthorizationCode
7376
}
@@ -100,7 +103,10 @@ func (r *RedisStore) SaveAuthorizationCode(ctx context.Context, code *core.Autho
100103
// GetAuthorizationCode retrieves an authorization code from Redis by client ID.
101104
// It returns ErrCodeNotFound if the code does not exist or has expired.
102105
// Uses client-side caching with 10 second TTL for better performance.
103-
func (r *RedisStore) GetAuthorizationCode(ctx context.Context, clientID string) (*core.AuthorizationCode, error) {
106+
func (r *RedisStore) GetAuthorizationCode(
107+
ctx context.Context,
108+
clientID string,
109+
) (*core.AuthorizationCode, error) {
104110
if clientID == "" {
105111
return nil, ErrEmptyClientID
106112
}

pkg/store/redis_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,11 @@ func TestRedisStore_UpdateClient(t *testing.T) {
564564
t.Errorf("GetClient() after update failed: %v", err)
565565
}
566566
if updated.Secret != tt.client.Secret {
567-
t.Errorf("UpdateClient() secret = %v, want %v", updated.Secret, tt.client.Secret)
567+
t.Errorf(
568+
"UpdateClient() secret = %v, want %v",
569+
updated.Secret,
570+
tt.client.Secret,
571+
)
568572
}
569573
}
570574
})
@@ -953,7 +957,11 @@ func TestRedisStore_GetClient_CacheInvalidation(t *testing.T) {
953957
t.Fatalf("GetClient() after update failed: %v", err)
954958
}
955959
if retrieved2.Scope != "read write admin" {
956-
t.Errorf("Retrieved client scope after update = %v, want %v", retrieved2.Scope, "read write admin")
960+
t.Errorf(
961+
"Retrieved client scope after update = %v, want %v",
962+
retrieved2.Scope,
963+
"read write admin",
964+
)
957965
}
958966

959967
t.Log("Cache invalidation working correctly after client update")

0 commit comments

Comments
 (0)