fix(cmd): ensure GITEA_HOST can be read #23

Merged
hiifong merged 2 commits from flynnhou/gitea-mcp:fix/docker-host-env-not-updated into main 2025-04-08 13:08:51 +00:00
Contributor

Why

With the following configuration:

docker run -i --rm -e GITEA_HOST=<gitea_host> -e GITEA_ACCESS_TOKEN=<gitea_access_token_for_host> 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

cmd/cmd.go Lines 74 to 77 in 7cfa1fa218
flagPkg.Host = host
if flagPkg.Host == "" {
flagPkg.Host = os.Getenv("GITEA_HOST")
}

Due to host could never be evaluated as an empty string from the default value "http://gitea.com"

cmd/cmd.go Lines 35 to 40 in 7cfa1fa218
flag.StringVar(
&host,
"host",
"https://gitea.com",
"Gitea host",
)

Unless user specify gitea-mcp ... --host <empty_string> ... with environment GITEA_HOST=<non_empty_string> 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
## Why With the following configuration: ```bash docker run -i --rm -e GITEA_HOST=<gitea_host> -e GITEA_ACCESS_TOKEN=<gitea_access_token_for_host> 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 <empty_string> ...` with environment `GITEA_HOST=<non_empty_string>` 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
flynnhou added 1 commit 2025-04-08 08:39:12 +00:00
fix(cmd): ensure GITEA_HOST can be read
All checks were successful
check-and-test / Run govulncheck (pull_request) Successful in 15s
check-and-test / check-and-test (pull_request) Successful in 18s
844c4dd625
flynnhou requested review from appleboy 2025-04-08 08:40:37 +00:00
flynnhou requested review from hiifong 2025-04-08 08:40:37 +00:00
flynnhou requested review from lunny 2025-04-08 08:40:38 +00:00
flynnhou requested review from yp05327 2025-04-08 08:40:38 +00:00
hiifong approved these changes 2025-04-08 13:08:25 +00:00
hiifong added 1 commit 2025-04-08 13:08:35 +00:00
Merge branch 'main' into fix/docker-host-env-not-updated
All checks were successful
check-and-test / Run govulncheck (pull_request) Successful in 13s
check-and-test / check-and-test (pull_request) Successful in 18s
9d00f676fd
hiifong merged commit f1b4a208a7 into main 2025-04-08 13:08:51 +00:00
hiifong deleted branch fix/docker-host-env-not-updated 2025-04-08 13:08:52 +00:00
Sign in to join this conversation.
No description provided.