import { kody } from 'kody:runtime'
export const NOTION_MCP_NAME = 'notion'
export const NOTION_MCP_URL = 'https://mcp.notion.com/mcp'
type McpServer = Record<string, (args?: Record<string, unknown>) => Promise<unknown>>
export function getNotionMcp(): McpServer | null {
const server = kody.mcp?.[NOTION_MCP_NAME]
if (!server || typeof server !== 'object') return null
const tools = Object.entries(server).filter(([, value]) => typeof value === 'function')
if (tools.length === 0) return null
return server as McpServer
}
export function isNotionMcpConnected(): boolean {
return getNotionMcp() !== null
}
export async function callNotionMcp(
candidates: Array<string>,
args: Record<string, unknown> = {},
): Promise<{ tool: string; result: unknown } | null> {
const server = getNotionMcp()
if (!server) return null
for (const name of candidates) {
const fn = server[name]
if (typeof fn === 'function') {
return { tool: name, result: await fn(args) }
}
}
return null
}
export function notionMcpSetupMessage(): string {
return [
'Add the official Notion MCP server, then authorize it:',
`kody.mcp_server_add({ name: "${NOTION_MCP_NAME}", url: "${NOTION_MCP_URL}" })`,
'Open the returned authUrl. Do not register an OAuth app for the one-click path.',
].join(' ')
}
export function firstArray(value: unknown): Array<unknown> | null {
if (Array.isArray(value)) return value
if (!value || typeof value !== 'object') return null
const record = value as Record<string, unknown>
for (const key of [
'results',
'items',
'data',
'issues',
'pages',
'customers',
'organizations',
'designs',
'values',
'nodes',
]) {
if (Array.isArray(record[key])) return record[key] as Array<unknown>
}
return null
}