MCPcopy Create free account
hub / github.com/Noumena-Network/code / createDirectConnectSession

Function createDirectConnectSession

src/server/createDirectConnectSession.ts:26–88  ·  view source on GitHub ↗
({
  serverUrl,
  authToken,
  cwd,
  dangerouslySkipPermissions,
}: {
  serverUrl: string
  authToken?: string
  cwd: string
  dangerouslySkipPermissions?: boolean
})

Source from the content-addressed store, hash-verified

24 * Throws DirectConnectError on network, HTTP, or response-parsing failures.
25 */
26export async function createDirectConnectSession({
27 serverUrl,
28 authToken,
29 cwd,
30 dangerouslySkipPermissions,
31}: {
32 serverUrl: string
33 authToken?: string
34 cwd: string
35 dangerouslySkipPermissions?: boolean
36}): Promise<{
37 config: DirectConnectConfig
38 workDir?: string
39}> {
40 const headers: Record<string, string> = {
41 'content-type': 'application/json',
42 }
43 if (authToken) {
44 headers['authorization'] = `Bearer ${authToken}`
45 }
46
47 let resp: Response
48 try {
49 resp = await fetch(`${serverUrl}/sessions`, {
50 method: 'POST',
51 headers,
52 body: jsonStringify({
53 cwd,
54 ...(dangerouslySkipPermissions && {
55 dangerously_skip_permissions: true,
56 }),
57 }),
58 })
59 } catch (err) {
60 throw new DirectConnectError(
61 `Failed to connect to server at ${serverUrl}: ${errorMessage(err)}`,
62 )
63 }
64
65 if (!resp.ok) {
66 throw new DirectConnectError(
67 `Failed to create session: ${resp.status} ${resp.statusText}`,
68 )
69 }
70
71 const result = connectResponseSchema().safeParse(await resp.json())
72 if (!result.success) {
73 throw new DirectConnectError(
74 `Invalid session response: ${result.error.message}`,
75 )
76 }
77
78 const data = result.data
79 return {
80 config: {
81 serverUrl,
82 sessionId: data.session_id,
83 wsUrl: data.ws_url,

Callers 1

runFunction · 0.85

Calls 3

fetchFunction · 0.85
jsonStringifyFunction · 0.85
errorMessageFunction · 0.50

Tested by

no test coverage detected