@pairdraw/mcp 0.0.0-stage → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +19 -2
- package/dist/connect.d.mts +1 -0
- package/dist/connect.mjs +192 -0
- package/dist/connect.mjs.map +1 -0
- package/dist/discovery-CB2Bv5WA.mjs +167 -0
- package/dist/discovery-CB2Bv5WA.mjs.map +1 -0
- package/dist/discovery.d.mts +59 -0
- package/dist/discovery.mjs +2 -0
- package/dist/index.d.mts +1 -0
- package/dist/index.mjs +95 -0
- package/dist/index.mjs.map +1 -0
- package/dist/metadata-DVz00c8R.d.mts +26 -0
- package/dist/paths-BpcXnrbP.mjs +168 -0
- package/dist/paths-BpcXnrbP.mjs.map +1 -0
- package/dist/policy-B8Ka4WRH.mjs +206 -0
- package/dist/policy-B8Ka4WRH.mjs.map +1 -0
- package/dist/rolldown-runtime-ClRpJifh.mjs +3 -0
- package/dist/root-BzmoiT2g.mjs +9 -0
- package/dist/root-BzmoiT2g.mjs.map +1 -0
- package/dist/scope-D2LTTsDw.mjs +66 -0
- package/dist/scope-D2LTTsDw.mjs.map +1 -0
- package/dist/scope-DJ9hsBYV.d.mts +31 -0
- package/dist/server--T-KD99G.mjs +3261 -0
- package/dist/server--T-KD99G.mjs.map +1 -0
- package/dist/server.d.mts +265 -0
- package/dist/server.mjs +2 -0
- package/dist/stdio.d.mts +1 -0
- package/dist/stdio.mjs +411 -0
- package/dist/stdio.mjs.map +1 -0
- package/dist/tools.d.mts +17 -0
- package/dist/tools.mjs +33 -0
- package/dist/tools.mjs.map +1 -0
- package/dist/transport.d.mts +59 -0
- package/dist/transport.mjs +2 -0
- package/package.json +64 -4
- package/src/agents/acp.ts +110 -0
- package/src/agents/claude-cloud.ts +99 -0
- package/src/agents/filter.ts +85 -0
- package/src/agents/index.ts +299 -0
- package/src/agents/install.ts +44 -0
- package/src/agents/link.ts +79 -0
- package/src/agents/lookup.ts +136 -0
- package/src/agents/pairing.ts +182 -0
- package/src/agents/path.ts +85 -0
- package/src/agents/process.ts +88 -0
- package/src/agents/registry.ts +36 -0
- package/src/agents/schema.ts +77 -0
- package/src/agents/sign-in.ts +106 -0
- package/src/agents/socket.ts +79 -0
- package/src/auth.ts +21 -0
- package/src/browser-rpc.ts +440 -0
- package/src/connect.ts +170 -0
- package/src/global.d.ts +5 -0
- package/src/http-options.ts +14 -0
- package/src/index.ts +179 -0
- package/src/json.ts +1 -0
- package/src/result.ts +37 -0
- package/src/root.ts +5 -0
- package/src/rpc-types.ts +5 -0
- package/src/server/connections.ts +58 -0
- package/src/server/files.ts +49 -0
- package/src/server/install-command.ts +24 -0
- package/src/server/lifecycle.ts +389 -0
- package/src/server/origin.ts +53 -0
- package/src/server/pending-requests.ts +62 -0
- package/src/server/sessions.ts +335 -0
- package/src/server.ts +597 -0
- package/src/skill/content.ts +40 -0
- package/src/skill/instructions.ts +69 -0
- package/src/skill/register.ts +77 -0
- package/src/stdio/bridge.ts +460 -0
- package/src/stdio.ts +130 -0
- package/src/tool/browser.ts +183 -0
- package/src/tool/index.ts +12 -0
- package/src/tool/manifest.ts +221 -0
- package/src/tool/metadata.ts +44 -0
- package/src/tool/output.ts +336 -0
- package/src/tool/policy.ts +37 -0
- package/src/tool/registration.ts +523 -0
- package/src/tool/scope.ts +71 -0
- package/src/transport/discovery.ts +235 -0
- package/src/transport/origins.ts +85 -0
- package/src/transport/paths.ts +179 -0
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import { access, constants, lstat, readFile, unlink } from 'node:fs/promises'
|
|
2
|
+
import { Socket } from 'node:net'
|
|
3
|
+
|
|
4
|
+
import * as v from 'valibot'
|
|
5
|
+
|
|
6
|
+
import { writeFileAtomic } from '@pairdraw/core/node/fs'
|
|
7
|
+
|
|
8
|
+
import { MCP_TOOL_SCOPES, type MCPToolScope } from '#mcp/tool/scope'
|
|
9
|
+
import { getDiscoveryPath, getSocketPath, platformHasUnixSockets } from '#mcp/transport/paths'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Metadata written to the discovery file so clients can auto-locate
|
|
13
|
+
* the running MCP server without knowing the socket path or TCP port.
|
|
14
|
+
*
|
|
15
|
+
* WARNING: The discovery file contains a plaintext auth token. Do not sync
|
|
16
|
+
* this file to cloud storage or include it in backups without encryption.
|
|
17
|
+
* Any process running as the same user can read this file.
|
|
18
|
+
*/
|
|
19
|
+
export interface DiscoveryInfo {
|
|
20
|
+
pid: number
|
|
21
|
+
/** Unix domain socket path, or null on platforms that don't support them (Windows). */
|
|
22
|
+
socketPath: string | null
|
|
23
|
+
httpPort: number
|
|
24
|
+
authRequired: boolean
|
|
25
|
+
authToken: string | null
|
|
26
|
+
version: string
|
|
27
|
+
startedAt: string
|
|
28
|
+
disabledTools?: string[]
|
|
29
|
+
/** What MCP clients can reach; stdio clients started outside the app follow it. */
|
|
30
|
+
scope?: MCPToolScope
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const discoveryInfoSchema = v.pipe(
|
|
34
|
+
v.string(),
|
|
35
|
+
v.parseJson(),
|
|
36
|
+
v.object({
|
|
37
|
+
pid: v.pipe(v.number(), v.integer(), v.minValue(1)),
|
|
38
|
+
version: v.string(),
|
|
39
|
+
httpPort: v.pipe(v.number(), v.integer(), v.minValue(0), v.maxValue(65535)),
|
|
40
|
+
authRequired: v.boolean(),
|
|
41
|
+
startedAt: v.string(),
|
|
42
|
+
socketPath: v.nullable(v.pipe(v.string(), v.nonEmpty())),
|
|
43
|
+
authToken: v.nullable(v.string()),
|
|
44
|
+
disabledTools: v.optional(v.array(v.string()), () => []),
|
|
45
|
+
scope: v.optional(v.picklist(MCP_TOOL_SCOPES), 'document')
|
|
46
|
+
})
|
|
47
|
+
) satisfies v.GenericSchema<string, DiscoveryInfo>
|
|
48
|
+
|
|
49
|
+
/** Parse discovery file contents, or null when the file is not valid discovery JSON. */
|
|
50
|
+
export function parseDiscoveryInfo(raw: string): DiscoveryInfo | null {
|
|
51
|
+
const result = v.safeParse(discoveryInfoSchema, raw)
|
|
52
|
+
return result.success ? result.output : null
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Writes the discovery JSON file at the platform-appropriate location.
|
|
57
|
+
* Overwrites any existing file (from a previous server instance).
|
|
58
|
+
*
|
|
59
|
+
* The file is written all at once with `0o600` permissions, replacing a symlink at the path
|
|
60
|
+
* rather than writing through it, so readers never see half a file and concurrent writers never
|
|
61
|
+
* interleave.
|
|
62
|
+
*
|
|
63
|
+
* WARNING: The auth token is written in plaintext. Do not sync the discovery
|
|
64
|
+
* file to cloud storage or include it in backups without encryption.
|
|
65
|
+
*/
|
|
66
|
+
export async function writeDiscoveryFile(info: DiscoveryInfo): Promise<void> {
|
|
67
|
+
const path = await getDiscoveryPath()
|
|
68
|
+
const json = JSON.stringify(info, null, 2)
|
|
69
|
+
await writeFileAtomic(path, json, { mode: 0o600 })
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Reads the discovery file. Returns null if:
|
|
74
|
+
* - The file does not exist
|
|
75
|
+
* - The file cannot be parsed
|
|
76
|
+
* - The recorded PID is no longer running (stale file)
|
|
77
|
+
*
|
|
78
|
+
* On success, returns the parsed DiscoveryInfo.
|
|
79
|
+
*/
|
|
80
|
+
export async function readDiscoveryFile(): Promise<DiscoveryInfo | null> {
|
|
81
|
+
const path = await getDiscoveryPath()
|
|
82
|
+
let raw: string
|
|
83
|
+
try {
|
|
84
|
+
raw = await readFile(path, 'utf-8')
|
|
85
|
+
} catch (e) {
|
|
86
|
+
if (!isEnoent(e))
|
|
87
|
+
process.stderr.write(
|
|
88
|
+
` Discovery: read warning (${e instanceof Error ? e.message : String(e)})\n`
|
|
89
|
+
)
|
|
90
|
+
return null
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const info = parseDiscoveryInfo(raw)
|
|
94
|
+
if (!info) return null
|
|
95
|
+
if (!isProcessAlive(info.pid)) return null
|
|
96
|
+
|
|
97
|
+
return info
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Removes the discovery file. Does not throw if the file does not exist.
|
|
102
|
+
*/
|
|
103
|
+
export async function removeDiscoveryFile(): Promise<void> {
|
|
104
|
+
const path = await getDiscoveryPath()
|
|
105
|
+
try {
|
|
106
|
+
await unlink(path)
|
|
107
|
+
} catch (e) {
|
|
108
|
+
if (!isEnoent(e)) throw e
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// TCP health check is more reliable than Unix socket connections in some
|
|
113
|
+
// runtimes (Bun on Linux). We read the discovery file directly instead of
|
|
114
|
+
// using readDiscoveryFile() to bypass the isProcessAlive PID check — the
|
|
115
|
+
// TCP fetch is a stronger liveness probe than process.kill(pid, 0).
|
|
116
|
+
async function isSocketLiveViaTcp(socketPath: string): Promise<boolean> {
|
|
117
|
+
let discoveryPath: string
|
|
118
|
+
try {
|
|
119
|
+
discoveryPath = await getDiscoveryPath()
|
|
120
|
+
} catch {
|
|
121
|
+
return false
|
|
122
|
+
}
|
|
123
|
+
const raw = await readFile(discoveryPath, 'utf-8').catch(() => null)
|
|
124
|
+
if (!raw) return false
|
|
125
|
+
const info = parseDiscoveryInfo(raw)
|
|
126
|
+
if (!info || info.socketPath !== socketPath) return false
|
|
127
|
+
|
|
128
|
+
// If TCP is disabled (httpPort <= 0), fall back to PID-based liveness check
|
|
129
|
+
if (info.httpPort <= 0) {
|
|
130
|
+
return isProcessAlive(info.pid)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return fetch(`http://127.0.0.1:${info.httpPort}/health`, {
|
|
134
|
+
signal: AbortSignal.timeout(2000)
|
|
135
|
+
})
|
|
136
|
+
.then(() => true)
|
|
137
|
+
.catch((e) => {
|
|
138
|
+
// A timeout means the process is likely alive but slow — don't delete its socket
|
|
139
|
+
if (e instanceof Error && e.name === 'TimeoutError') return true
|
|
140
|
+
return false
|
|
141
|
+
})
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Direct Unix-socket connection probe. A server binds the socket before
|
|
146
|
+
* writeDiscovery(), so isSocketLiveViaTcp() can return false during that
|
|
147
|
+
* window. This probe connects directly to the socket path — a successful
|
|
148
|
+
* connection or timeout means the socket is live; only ECONNREFUSED means
|
|
149
|
+
* no process is listening.
|
|
150
|
+
*/
|
|
151
|
+
function probeSocketLive(socketPath: string): Promise<boolean> {
|
|
152
|
+
return new Promise((resolve) => {
|
|
153
|
+
let settled = false
|
|
154
|
+
const socket = new Socket()
|
|
155
|
+
const finish = (result: boolean) => {
|
|
156
|
+
if (settled) return
|
|
157
|
+
settled = true
|
|
158
|
+
clearTimeout(timer)
|
|
159
|
+
socket.destroy()
|
|
160
|
+
// oxlint-disable-next-line no-multiple-resolved -- settled flag guarantees single resolution
|
|
161
|
+
resolve(result)
|
|
162
|
+
}
|
|
163
|
+
const timer = setTimeout(() => finish(true), 1000)
|
|
164
|
+
socket.once('connect', () => finish(true))
|
|
165
|
+
socket.once('error', (err) => {
|
|
166
|
+
const code = (err as NodeJS.ErrnoException).code
|
|
167
|
+
finish(code !== 'ECONNREFUSED')
|
|
168
|
+
})
|
|
169
|
+
socket.connect(socketPath)
|
|
170
|
+
})
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Removes a stale Unix domain socket file if it exists and is not live.
|
|
175
|
+
* A socket is considered stale if no process is listening on it.
|
|
176
|
+
*/
|
|
177
|
+
export async function removeStaleSocket(socketPathOverride?: string): Promise<void> {
|
|
178
|
+
if (!platformHasUnixSockets()) return
|
|
179
|
+
|
|
180
|
+
const socketPath = socketPathOverride ?? (await getSocketPath())
|
|
181
|
+
let exists: boolean
|
|
182
|
+
try {
|
|
183
|
+
await access(socketPath, constants.F_OK)
|
|
184
|
+
exists = true
|
|
185
|
+
} catch {
|
|
186
|
+
exists = false
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (!exists) return
|
|
190
|
+
|
|
191
|
+
// Verify the path is actually a socket before unlinking it.
|
|
192
|
+
// A misconfigured PAIRDRAW_MCP_SOCKET could point at a regular file;
|
|
193
|
+
// we must never delete non-socket paths.
|
|
194
|
+
const stat = await lstat(socketPath).catch((e) => {
|
|
195
|
+
if (isEnoent(e)) return null
|
|
196
|
+
throw e
|
|
197
|
+
})
|
|
198
|
+
if (!stat) return
|
|
199
|
+
if (!stat.isSocket()) {
|
|
200
|
+
throw new Error(`Refusing to remove non-socket path: ${socketPath}`)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (await probeSocketLive(socketPath)) return
|
|
204
|
+
if (await isSocketLiveViaTcp(socketPath)) return
|
|
205
|
+
|
|
206
|
+
// No live server claims this socket — remove the stale file.
|
|
207
|
+
try {
|
|
208
|
+
await unlink(socketPath)
|
|
209
|
+
} catch (e) {
|
|
210
|
+
if (e instanceof Error && 'code' in e && (e as NodeJS.ErrnoException).code !== 'ENOENT') {
|
|
211
|
+
process.stderr.write(`Failed to remove stale socket: ${e.message}\n`)
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Checks if a process with the given PID is still running.
|
|
218
|
+
* Uses process.kill(pid, 0) which doesn't send a signal, just checks existence.
|
|
219
|
+
* Works on both Unix and Windows.
|
|
220
|
+
*/
|
|
221
|
+
function isProcessAlive(pid: number): boolean {
|
|
222
|
+
try {
|
|
223
|
+
process.kill(pid, 0)
|
|
224
|
+
return true
|
|
225
|
+
} catch (e) {
|
|
226
|
+
if (e instanceof Error && 'code' in e && (e as NodeJS.ErrnoException).code === 'ESRCH') {
|
|
227
|
+
return false
|
|
228
|
+
}
|
|
229
|
+
return true
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function isEnoent(e: unknown): boolean {
|
|
234
|
+
return e instanceof Error && 'code' in e && (e as NodeJS.ErrnoException).code === 'ENOENT'
|
|
235
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { uniq } from 'es-toolkit'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Origins allowed to call the local server from a view.
|
|
5
|
+
*
|
|
6
|
+
* The desktop app's webview always calls the server from its own origin, so a
|
|
7
|
+
* manually started server must allow it without extra configuration. Browsers
|
|
8
|
+
* set `Origin` themselves, and only the Tauri webview serves these origins, so
|
|
9
|
+
* allowing them does not expose the server to arbitrary websites. Requests still
|
|
10
|
+
* need the bearer token whenever authentication is enabled.
|
|
11
|
+
*/
|
|
12
|
+
export const DESKTOP_APP_ORIGINS = [
|
|
13
|
+
'tauri://localhost',
|
|
14
|
+
'http://tauri.localhost',
|
|
15
|
+
'https://tauri.localhost'
|
|
16
|
+
] as const
|
|
17
|
+
|
|
18
|
+
/** Parse a comma-separated override. Returns null when nothing usable is set. */
|
|
19
|
+
export function parseCORSOrigins(raw: string | undefined | null): string[] | null {
|
|
20
|
+
if (raw == null) return null
|
|
21
|
+
const origins = raw
|
|
22
|
+
.split(',')
|
|
23
|
+
.map((origin) => origin.trim())
|
|
24
|
+
.filter((origin) => origin.length > 0)
|
|
25
|
+
return origins.length > 0 ? origins : null
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Explicit configuration wins; otherwise the desktop app origin is allowed. */
|
|
29
|
+
export function resolveCORSOrigins(raw: string | undefined): readonly string[] {
|
|
30
|
+
return parseCORSOrigins(raw) ?? DESKTOP_APP_ORIGINS
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The web app a companion started by `pairdraw-connect` pairs with unless the operator names
|
|
35
|
+
* another: the editor `bun run dev` serves on this computer. Pairdraw hosts no web app, so no
|
|
36
|
+
* site elsewhere is trusted by default.
|
|
37
|
+
*/
|
|
38
|
+
export const DEFAULT_CONNECT_APP_URL = 'http://localhost:1420'
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Whether a request may reach the server from where it came. Browsers send `Origin` on every
|
|
42
|
+
* WebSocket upgrade and cross-site request, so a page from any other site, including one that
|
|
43
|
+
* rebinds its DNS name to this machine, is refused. Local programs send none and are allowed;
|
|
44
|
+
* they still need a token.
|
|
45
|
+
*/
|
|
46
|
+
export function isAllowedOrigin(
|
|
47
|
+
origin: string | undefined | null,
|
|
48
|
+
allowed: readonly string[]
|
|
49
|
+
): boolean {
|
|
50
|
+
if (origin === undefined || origin === null) return true
|
|
51
|
+
// An operator who sets `*` opted out of the check, as for CORS.
|
|
52
|
+
return allowed.includes(origin) || allowed.includes('*')
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The origins the server admits: the configured ones and, always, the desktop app's.
|
|
57
|
+
* With the agent bridge on, `*` is dropped: it would let any website drive the bridge, so it
|
|
58
|
+
* is ignored ("an operator who sets `*` opted out" applies only without the bridge).
|
|
59
|
+
*/
|
|
60
|
+
export function admittedOrigins(
|
|
61
|
+
corsOrigin: string | readonly string[] | null,
|
|
62
|
+
options?: { withAgents?: boolean }
|
|
63
|
+
): string[] {
|
|
64
|
+
const configured =
|
|
65
|
+
(typeof corsOrigin === 'string' ? parseCORSOrigins(corsOrigin) : corsOrigin) ?? []
|
|
66
|
+
const usable = options?.withAgents ? configured.filter((origin) => origin !== '*') : configured
|
|
67
|
+
return uniq([...usable, ...DESKTOP_APP_ORIGINS])
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Whether a request's `Host` names this machine: `127.0.0.1`, `localhost`, `[::1]`, or a
|
|
72
|
+
* `*.localhost` name, which resolve only to loopback. A page that rebinds its own DNS name to
|
|
73
|
+
* 127.0.0.1 still sends that name, so it is refused. Requests without one are allowed.
|
|
74
|
+
*/
|
|
75
|
+
export function isLoopbackHost(host: string | undefined | null): boolean {
|
|
76
|
+
if (host === undefined || host === null || host === '') return true
|
|
77
|
+
const hostname = /^(\[[^\]]*\]|[^:]*)(?::\d+)?$/.exec(host.trim().toLowerCase())?.[1]
|
|
78
|
+
if (!hostname) return false
|
|
79
|
+
return (
|
|
80
|
+
hostname === '127.0.0.1' ||
|
|
81
|
+
hostname === 'localhost' ||
|
|
82
|
+
hostname === '[::1]' ||
|
|
83
|
+
hostname.endsWith('.localhost')
|
|
84
|
+
)
|
|
85
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { chmod, mkdir } from 'node:fs/promises'
|
|
2
|
+
import { homedir, platform } from 'node:os'
|
|
3
|
+
import { dirname, join } from 'node:path'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Platform-specific paths for the MCP server's Unix domain socket
|
|
7
|
+
* and the discovery JSON file.
|
|
8
|
+
*
|
|
9
|
+
* Socket directory layout (overridable via PAIRDRAW_MCP_SOCKET):
|
|
10
|
+
* macOS: ~/Library/Application Support/Pairdraw/
|
|
11
|
+
* Linux: $XDG_RUNTIME_DIR/pairdraw/ (fallback: ~/.pairdraw/)
|
|
12
|
+
* Windows: %LOCALAPPDATA%\Pairdraw\ (fallback: ~\AppData\Local\Pairdraw\)
|
|
13
|
+
*
|
|
14
|
+
* On Windows, Unix domain sockets are unavailable — the server uses TCP only.
|
|
15
|
+
*
|
|
16
|
+
* Discovery file: at the platform-default path above, UNLESS
|
|
17
|
+
* PAIRDRAW_MCP_DISCOVERY_PATH is set (see getDiscoveryPath()). The socket
|
|
18
|
+
* override (PAIRDRAW_MCP_SOCKET) never moves the discovery file — it is
|
|
19
|
+
* recorded in the discovery file's `socketPath` field so clients read it
|
|
20
|
+
* from the well-known location.
|
|
21
|
+
* Socket file: <socketDir>/mcp.sock (or the override path)
|
|
22
|
+
*
|
|
23
|
+
* IMPORTANT: getSocketDir() returns the directory that contains the socket
|
|
24
|
+
* file. It does NOT always contain the discovery file — when
|
|
25
|
+
* PAIRDRAW_MCP_SOCKET is set, the discovery file stays at getPlatformDir().
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
const DIR_NAME_UNIX = 'pairdraw'
|
|
29
|
+
const DIR_NAME_MACOS = 'Pairdraw'
|
|
30
|
+
const SOCKET_FILENAME = 'mcp.sock'
|
|
31
|
+
const DISCOVERY_FILENAME = 'mcp.json'
|
|
32
|
+
|
|
33
|
+
const isMacOS = platform() === 'darwin'
|
|
34
|
+
const isWindows = platform() === 'win32'
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Returns the platform-specific default directory for MCP runtime files,
|
|
38
|
+
* ignoring PAIRDRAW_MCP_SOCKET. The discovery file always lives here so
|
|
39
|
+
* clients can find it at a well-known location regardless of socket overrides.
|
|
40
|
+
* Creates the directory (with restrictive permissions) if it does not exist.
|
|
41
|
+
*
|
|
42
|
+
* The 0o700 mode restricts permissions on Unix. On Windows, mkdir ignores
|
|
43
|
+
* the mode and uses default ACLs — directory access is controlled by the
|
|
44
|
+
* filesystem, not the permission bits.
|
|
45
|
+
*/
|
|
46
|
+
async function getPlatformDir(): Promise<string> {
|
|
47
|
+
let dir: string
|
|
48
|
+
|
|
49
|
+
if (isMacOS) {
|
|
50
|
+
dir = join(homedir(), 'Library', 'Application Support', DIR_NAME_MACOS)
|
|
51
|
+
} else if (isWindows) {
|
|
52
|
+
const localAppData = process.env.LOCALAPPDATA?.trim()
|
|
53
|
+
if (localAppData) {
|
|
54
|
+
dir = join(localAppData, DIR_NAME_MACOS)
|
|
55
|
+
} else {
|
|
56
|
+
dir = join(homedir(), 'AppData', 'Local', DIR_NAME_MACOS)
|
|
57
|
+
}
|
|
58
|
+
} else {
|
|
59
|
+
// Linux / other Unix
|
|
60
|
+
const xdgRuntime = process.env.XDG_RUNTIME_DIR?.trim()
|
|
61
|
+
if (xdgRuntime) {
|
|
62
|
+
dir = join(xdgRuntime, DIR_NAME_UNIX)
|
|
63
|
+
} else {
|
|
64
|
+
dir = join(homedir(), `.${DIR_NAME_UNIX}`)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
await mkdir(dir, { recursive: true, mode: 0o700 })
|
|
69
|
+
if (!isWindows) await chmod(dir, 0o700)
|
|
70
|
+
return dir
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Returns the directory for the MCP socket file.
|
|
75
|
+
*
|
|
76
|
+
* When PAIRDRAW_MCP_SOCKET is set, its dirname is used as the socket
|
|
77
|
+
* directory. When unset, the platform default from getPlatformDir() is used.
|
|
78
|
+
* Creates the directory (with restrictive permissions) if it does not exist.
|
|
79
|
+
*
|
|
80
|
+
* NOTE: The discovery file always lives at getPlatformDir(), regardless of
|
|
81
|
+
* PAIRDRAW_MCP_SOCKET. This function should NOT be used to locate it.
|
|
82
|
+
*/
|
|
83
|
+
export async function getSocketDir(): Promise<string> {
|
|
84
|
+
const socketOverride = process.env.PAIRDRAW_MCP_SOCKET?.trim()
|
|
85
|
+
|
|
86
|
+
if (socketOverride) {
|
|
87
|
+
const dir = dirname(socketOverride)
|
|
88
|
+
// Create missing parents with restrictive permissions. Do NOT chmod
|
|
89
|
+
// existing directories — the override path is user-controlled and
|
|
90
|
+
// may be in a shared directory (e.g. /tmp for tests).
|
|
91
|
+
await mkdir(dir, { recursive: true, mode: 0o700 })
|
|
92
|
+
return dir
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return getPlatformDir()
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Returns the full path to the MCP Unix domain socket.
|
|
100
|
+
*
|
|
101
|
+
* On macOS/Linux: <socketDir>/mcp.sock
|
|
102
|
+
*
|
|
103
|
+
* When PAIRDRAW_MCP_SOCKET is set, its value is returned directly
|
|
104
|
+
* (no directory resolution needed).
|
|
105
|
+
*/
|
|
106
|
+
export async function getSocketPath(): Promise<string> {
|
|
107
|
+
const socketOverride = process.env.PAIRDRAW_MCP_SOCKET?.trim()
|
|
108
|
+
if (socketOverride) {
|
|
109
|
+
// Ensure the override directory exists. getSocketDir() creates the
|
|
110
|
+
// directory for the custom socket path (dirname of the override).
|
|
111
|
+
await getSocketDir()
|
|
112
|
+
return socketOverride
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const dir = await getSocketDir()
|
|
116
|
+
return join(dir, SOCKET_FILENAME)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Returns the full path to the MCP discovery JSON file.
|
|
121
|
+
*
|
|
122
|
+
* The discovery file lives at the platform-default location so clients can
|
|
123
|
+
* find it without knowing whether PAIRDRAW_MCP_SOCKET is set. It contains
|
|
124
|
+
* the actual socket path (which may be overridden) in its `socketPath` field,
|
|
125
|
+
* so clients read the discovery file to learn where to connect — not the other
|
|
126
|
+
* way around.
|
|
127
|
+
*
|
|
128
|
+
* Set PAIRDRAW_MCP_DISCOVERY_PATH to relocate the discovery file (e.g. to a
|
|
129
|
+
* temp directory for test isolation). The desktop app reads the platform
|
|
130
|
+
* default via its own path computation (src/app/automation/mcp/spawn.ts) and
|
|
131
|
+
* does not honor this override, so it is primarily useful for tests and
|
|
132
|
+
* standalone server invocations. The parent directory is created (0o700) so
|
|
133
|
+
* writeDiscoveryFile's atomic temp-then-rename succeeds.
|
|
134
|
+
*/
|
|
135
|
+
export async function getDiscoveryPath(): Promise<string> {
|
|
136
|
+
const override = process.env.PAIRDRAW_MCP_DISCOVERY_PATH?.trim()
|
|
137
|
+
if (override) {
|
|
138
|
+
const dir = dirname(override)
|
|
139
|
+
await mkdir(dir, { recursive: true, mode: 0o700 })
|
|
140
|
+
return override
|
|
141
|
+
}
|
|
142
|
+
const dir = await getPlatformDir()
|
|
143
|
+
return join(dir, DISCOVERY_FILENAME)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** The private folder (0700) the companion keeps its pairing pages in, beside the discovery file. */
|
|
147
|
+
export async function getCompanionDir(): Promise<string> {
|
|
148
|
+
const dir = join(await getPlatformDir(), 'connect')
|
|
149
|
+
await mkdir(dir, { recursive: true, mode: 0o700 })
|
|
150
|
+
if (!isWindows) await chmod(dir, 0o700)
|
|
151
|
+
return dir
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* The folder coding agents start in when the companion runs them, beside the discovery file and
|
|
156
|
+
* outside the person's projects, whose settings an agent would otherwise read.
|
|
157
|
+
*/
|
|
158
|
+
export async function getAgentWorkspacePath(): Promise<string> {
|
|
159
|
+
return join(await getPlatformDir(), 'agent-workspace')
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Returns true if the current platform supports Unix domain sockets.
|
|
164
|
+
* Unix domain sockets are available on macOS, Linux, and other POSIX
|
|
165
|
+
* platforms but not on native Windows. WSL is detected as Linux.
|
|
166
|
+
*/
|
|
167
|
+
export function platformHasUnixSockets(): boolean {
|
|
168
|
+
return !isWindows
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Returns the platform name for display purposes.
|
|
173
|
+
*/
|
|
174
|
+
export function platformName(): 'macos' | 'linux' | 'windows' | 'other' {
|
|
175
|
+
if (isMacOS) return 'macos'
|
|
176
|
+
if (isWindows) return 'windows'
|
|
177
|
+
if (platform() === 'linux') return 'linux'
|
|
178
|
+
return 'other'
|
|
179
|
+
}
|