Skip to content

Commit 6efe91e

Browse files
committed
feat: resolves CWD variable in MCP server configs
1 parent 5a11222 commit 6efe91e

3 files changed

Lines changed: 19 additions & 17 deletions

File tree

.mcphub/servers.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"args": [
55
"-y",
66
"@modelcontextprotocol/server-filesystem",
7-
"${PWD}"
7+
"${CWD}"
88
],
99
"command": "npx"
1010
},
@@ -21,7 +21,7 @@
2121
"lsp": {
2222
"args": [
2323
"--workspace",
24-
"${PWD}",
24+
"${CWD}",
2525
"--lsp",
2626
"lua-language-server",
2727
"--",

doc/workspace.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Consider these common scenarios:
1616
"mcpServers": {
1717
"filesystem": {
1818
"command": "uvx",
19-
"args": ["mcp-server-filesystem", "${PWD}"]
19+
"args": ["mcp-server-filesystem", "${CWD}"]
2020
}
2121
}
2222
}
@@ -29,7 +29,7 @@ Consider these common scenarios:
2929
"lsp": {
3030
"command": "mcp-language-server",
3131
"args": [
32-
"--workspace", "${PWD}",
32+
"--workspace", "${CWD}",
3333
"--lsp", "typescript-language-server",
3434
"--", "--stdio"
3535
]
@@ -40,6 +40,7 @@ Consider these common scenarios:
4040

4141
Without workspaces, you'd need to manually edit the global config for every project. With workspaces, each project gets its own configuration automatically.
4242

43+
4344
## How It Works
4445

4546
#### 1. Project Detection
@@ -102,12 +103,12 @@ require("mcphub").setup({
102103
"mcpServers": {
103104
"filesystem": {
104105
"command": "uvx",
105-
"args": ["mcp-server-filesystem", "${PWD}"]
106+
"args": ["mcp-server-filesystem", "${CWD}"]
106107
},
107108
"lsp": {
108109
"command": "mcp-language-server",
109110
"args": [
110-
"--workspace", "${PWD}",
111+
"--workspace", "${CWD}",
111112
"--lsp", "lua-language-server",
112113
"--", "--stdio"
113114
]
@@ -118,18 +119,19 @@ require("mcphub").setup({
118119

119120
#### Environment Variables
120121

121-
Variables like `${PWD}` are automatically available since the hub starts with the project directory as its working directory. Basic environment variables (`HOME`, `USER`, `TERM`, `SHELL`, etc.) are always available.
122+
`CWD` will be set to the working directory of the current running `mcp-hub`. Basic environment variables (`HOME`, `USER`, `TERM`, `SHELL`, etc.) are always available.
122123

123124
Additional variables can be set via:
124125
1. **global_env** configuration in MCP Hub setup
125126
2. **Process environment** when the hub starts
126127

127128
```lua
128129
require("mcphub").setup({
129-
global_env = {
130-
"CUSTOM_VAR", -- Uses $CUSTOM_VAR from the environment
131-
"DBUS_SESSION_BUS_ADDRESS" = os.getenv("DBUS_SESSION_BUS_ADDRESS") -- Uses os.getenv
132-
}
130+
global_env = function(context)
131+
return {
132+
DBUS_SESSION_BUS_ADDRESS = os.getenv("DBUS_SESSION_BUS_ADDRESS") or "",
133+
}
134+
end
133135
})
134136
```
135137

lua/mcphub/hub.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ end
7777
--- Resolve workspace-specific context
7878
--- @return MCPHub.JobContext|nil Workspace context or nil to fall back to global
7979
function MCPHub:_resolve_workspace_context()
80-
local current_dir = vim.fn.getcwd()
80+
local cwd = vim.fn.getcwd()
8181

8282
-- Find workspace config
83-
local workspace_info = workspace_utils.find_workspace_config(State.config.workspace.look_for, current_dir)
83+
local workspace_info = workspace_utils.find_workspace_config(State.config.workspace.look_for, cwd)
8484

8585
if not workspace_info then
8686
-- No workspace config found, fall back to global mode
@@ -137,10 +137,10 @@ function MCPHub:_resolve_workspace_context()
137137

138138
return {
139139
port = port,
140-
cwd = workspace_info.root_dir,
140+
cwd = workspace_info.root_dir or cwd,
141141
config_files = config_files,
142142
is_workspace_mode = true,
143-
workspace_root = workspace_info.root_dir,
143+
workspace_root = workspace_info.root_dir or cwd,
144144
existing_hub = existing_hub, -- Include hub info from cache
145145
}
146146
end
@@ -157,7 +157,7 @@ function MCPHub:_resolve_global_context()
157157
cwd = cwd,
158158
config_files = { self.config },
159159
is_workspace_mode = false,
160-
workspace_root = nil,
160+
workspace_root = cwd,
161161
existing_hub = existing_hub,
162162
}
163163
end
@@ -256,7 +256,7 @@ function MCPHub:_start_server_with_context(context)
256256
vim.notify("Failed to serialize global_env: " .. json_env, vim.log.levels.WARN)
257257
end
258258
end
259-
job_env = vim.tbl_extend("force", vim.fn.environ(), job_env or {})
259+
job_env = vim.tbl_extend("force", vim.fn.environ(), { CWD = context.cwd }, job_env or {})
260260

261261
log.debug("Starting server with context" .. vim.inspect({
262262
port = context.port,

0 commit comments

Comments
 (0)