MCPcopy Create free account
hub / github.com/ZenNotes/zennotes / runMcpServer

Function runMcpServer

apps/desktop/src/mcp/server.ts:794–844  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

792/* ---------- Server plumbing ------------------------------------------ */
793
794export async function runMcpServer(): Promise<void> {
795 // Resolve the vault up front so we fail fast with a clear error
796 // instead of surprising every tool call. When the user hasn't picked
797 // a vault yet we still boot so the client surface stays consistent,
798 // but every tool call will report the missing-vault error.
799 let vaultPromise: Promise<string> | null = null
800 const getVault = (): Promise<string> => {
801 if (!vaultPromise) vaultPromise = resolveVaultRoot()
802 return vaultPromise
803 }
804
805 const instructions = await resolveInstructions()
806 const server = new Server(
807 { name: 'zennotes', version: '0.1.0' },
808 {
809 capabilities: { tools: {} },
810 instructions
811 }
812 )
813
814 server.setRequestHandler(ListToolsRequestSchema, async () => ({
815 tools: TOOLS.map((t) => t.schema)
816 }))
817
818 server.setRequestHandler(CallToolRequestSchema, async (request) => {
819 const { name, arguments: args } = request.params
820 const tool = TOOLS.find((t) => t.schema.name === name)
821 if (!tool) {
822 return {
823 content: [{ type: 'text', text: `Unknown tool: ${name}` }],
824 isError: true
825 }
826 }
827 try {
828 const vault = await getVault()
829 const result = await tool.handler((args ?? {}) as Record<string, unknown>, vault)
830 const payload =
831 typeof result === 'string' ? result : JSON.stringify(result, null, 2)
832 return { content: [{ type: 'text', text: payload }] }
833 } catch (err) {
834 const message = err instanceof Error ? err.message : String(err)
835 return {
836 content: [{ type: 'text', text: `Error: ${message}` }],
837 isError: true
838 }
839 }
840 })
841
842 const transport = new StdioServerTransport()
843 await server.connect(transport)
844}

Callers 2

cmdMcpFunction · 0.85
index.tsFile · 0.85

Calls 4

resolveInstructionsFunction · 0.85
getVaultFunction · 0.85
handlerMethod · 0.80
connectMethod · 0.45

Tested by

no test coverage detected