Skip to content

Commit 920974b

Browse files
committed
Add debug logging for GraphQL requests and responses, controlled by FORGE_DEBUG environment variable
1 parent 5fa5b49 commit 920974b

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ A lightweight, configuration-driven MCP server that exposes curated GraphQL quer
1010

1111
## Configuration
1212

13-
To configure the MCP server, specify the environment variable `FORGE_CONFIG` pointing to the folder containing the YAML configuration files.
13+
The server is configured using environment variables and YAML files.
14+
15+
### Environment Variables
16+
17+
- `FORGE_CONFIG`: Specifies the path to the folder containing the YAML configuration files (`forge.yaml` and tool definitions). Defaults to the current directory (`.`) if not set.
18+
- `FORGE_DEBUG`: If set to `true` (case-insensitive), enables detailed debug logging to `stderr`, including the obtained token and the full HTTP request/response for GraphQL calls. Defaults to `false`.
1419

1520
### forge.yaml
1621

main.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ import (
66
"encoding/json"
77
"fmt"
88
"io"
9+
"log"
910
"net/http"
11+
"net/http/httputil"
1012
"os"
1113
"os/exec"
1214
"path/filepath"
1315
"runtime"
16+
"strconv"
1417

1518
"gopkg.in/yaml.v3"
1619

@@ -45,6 +48,8 @@ type graphqlRequest struct {
4548
Variables map[string]interface{} `json:"variables,omitempty"`
4649
}
4750

51+
var isDebug bool
52+
4853
// loadConfig reads YAML from path into out
4954
func loadConfig(path string, out interface{}) error {
5055
data, err := os.ReadFile(path)
@@ -72,6 +77,17 @@ func executeGraphQL(url, query string, vars map[string]interface{}, token string
7277
req.Header.Set("Authorization", "Bearer "+token)
7378
}
7479

80+
if isDebug {
81+
log.Println("--- GraphQL Request ---")
82+
reqDump, dumpErr := httputil.DumpRequestOut(req, true)
83+
if dumpErr != nil {
84+
log.Printf("Error dumping request: %v\n", dumpErr)
85+
} else {
86+
log.Printf("%s\n", string(reqDump))
87+
}
88+
log.Println("-----------------------")
89+
}
90+
7591
resp, err := http.DefaultClient.Do(req)
7692
if err != nil {
7793
return nil, fmt.Errorf("execute request: %w", err)
@@ -82,6 +98,21 @@ func executeGraphQL(url, query string, vars map[string]interface{}, token string
8298
if err != nil {
8399
return nil, fmt.Errorf("read response: %w", err)
84100
}
101+
102+
if isDebug {
103+
log.Println("--- GraphQL Response ---")
104+
log.Printf("Status Code: %d\n", resp.StatusCode)
105+
// Attempt to pretty-print JSON response body if possible
106+
var prettyJSON bytes.Buffer
107+
if jsonErr := json.Indent(&prettyJSON, respBody, "", " "); jsonErr == nil {
108+
log.Printf("Body:\n%s\n", prettyJSON.String())
109+
} else {
110+
// Fallback to printing raw body if not valid JSON
111+
log.Printf("Body (raw):\n%s\n", string(respBody))
112+
}
113+
log.Println("------------------------")
114+
}
115+
85116
return respBody, nil
86117
}
87118

@@ -130,6 +161,9 @@ func makeHandler(cfg ForgeConfig, tcfg ToolConfig) server.ToolHandlerFunc {
130161
return mcp.NewToolResultErrorFromErr(errMsg, err), nil
131162
}
132163
token = string(bytes.TrimSpace(out))
164+
if isDebug {
165+
log.Printf("Obtained token: %s\n", token) // Log the token if debug is enabled
166+
}
133167
}
134168

135169
// 3. Call GraphQL
@@ -151,6 +185,18 @@ func main() {
151185
configDir = "." // Default to current directory if not set
152186
}
153187

188+
// Check for FORGE_DEBUG environment variable
189+
debugEnv := os.Getenv("FORGE_DEBUG")
190+
isDebug, _ = strconv.ParseBool(debugEnv) // Ignore error, defaults to false if not set or invalid
191+
192+
if isDebug {
193+
log.SetOutput(os.Stderr) // Ensure logs go to stderr
194+
log.Println("Debug mode enabled.")
195+
} else {
196+
// Disable logging if not in debug mode
197+
log.SetOutput(io.Discard)
198+
}
199+
154200
// Load forge.yaml
155201
var cfg ForgeConfig
156202
forgeConfigPath := filepath.Join(configDir, "forge.yaml")

0 commit comments

Comments
 (0)