From f1b4a208a7084303d9f1ec615907f876a760d446 Mon Sep 17 00:00:00 2001 From: Flynn Hou Date: Tue, 8 Apr 2025 13:08:50 +0000 Subject: [PATCH] fix(cmd): ensure GITEA_HOST can be read (#23) ## Why With the following configuration: ```bash docker run -i --rm -e GITEA_HOST= -e GITEA_ACCESS_TOKEN= docker.gitea.com/gitea-mcp-server:latest ``` after mcp-client calling a tool, the gitea client will encounter the following fatal error: ``` FATAL gitea/gitea.go:47 create gitea client err: user does not exist [uid: 0, name: ] gitea.com/gitea/gitea-mcp/pkg/gitea.Client.func1 /app/pkg/gitea/gitea.go:47 sync.(*Once).doSlow /usr/local/go/src/sync/once.go:78 sync.(*Once).Do /usr/local/go/src/sync/once.go:69 gitea.com/gitea/gitea-mcp/pkg/gitea.Client /app/pkg/gitea/gitea.go:21 gitea.com/gitea/gitea-mcp/operation/search.SearchReposFn /app/operation/search/search.go:161 github.com/mark3labs/mcp-go/server.(*MCPServer).handleToolCall /go/pkg/mod/github.com/mark3labs/mcp-go@v0.18.0/server/server.go:717 github.com/mark3labs/mcp-go/server.(*MCPServer).HandleMessage /go/pkg/mod/github.com/mark3labs/mcp-go@v0.18.0/server/request_handler.go:264 github.com/mark3labs/mcp-go/server.(*StdioServer).processMessage /go/pkg/mod/github.com/mark3labs/mcp-go@v0.18.0/server/stdio.go:228 github.com/mark3labs/mcp-go/server.(*StdioServer).processInputStream /go/pkg/mod/github.com/mark3labs/mcp-go@v0.18.0/server/stdio.go:143 github.com/mark3labs/mcp-go/server.(*StdioServer).Listen /go/pkg/mod/github.com/mark3labs/mcp-go@v0.18.0/server/stdio.go:209 github.com/mark3labs/mcp-go/server.ServeStdio /go/pkg/mod/github.com/mark3labs/mcp-go@v0.18.0/server/stdio.go:282 gitea.com/gitea/gitea-mcp/operation.Run /app/operation/operation.go:48 gitea.com/gitea/gitea-mcp/cmd.Execute /app/cmd/cmd.go:119 main.main /app/main.go:12 runtime.main /usr/local/go/src/runtime/proc.go:283 ``` Turns out the root cause was because the `GITEA_HOST` environment variable wasn't overriding the default flag value, resulting in mismatch of host and access token. The if statement won't be entered https://gitea.com/gitea/gitea-mcp/src/commit/7cfa1fa218654e14c0ed0f9bac70ef75039f6178/cmd/cmd.go#L74-L77 Due to `host` could never be evaluated as an empty string from the default value `"http://gitea.com"` https://gitea.com/gitea/gitea-mcp/src/commit/7cfa1fa218654e14c0ed0f9bac70ef75039f6178/cmd/cmd.go#L35-L40 Unless user specify `gitea-mcp ... --host ...` with environment `GITEA_HOST=` at the same time, which is very unlikely IMHO. ## How - Set `host` flag default value from `GITEA_HOST` environment variable value - Remove possible dead code if-statement Co-authored-by: hiifong Reviewed-on: https://gitea.com/gitea/gitea-mcp/pulls/23 Reviewed-by: hiifong Co-authored-by: Flynn Hou Co-committed-by: Flynn Hou --- cmd/cmd.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 30730a7..2481103 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -35,7 +35,7 @@ func init() { flag.StringVar( &host, "host", - "https://gitea.com", + os.Getenv("GITEA_HOST"), "Gitea host", ) flag.IntVar( @@ -72,9 +72,6 @@ func init() { flag.Parse() flagPkg.Host = host - if flagPkg.Host == "" { - flagPkg.Host = os.Getenv("GITEA_HOST") - } if flagPkg.Host == "" { flagPkg.Host = "https://gitea.com" }