Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/init-server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

import { OpenAPIV3 } from 'openapi-types'
import OpenAPISchemaValidator from 'openapi-schema-validator'
Expand Down Expand Up @@ -42,9 +43,24 @@ async function loadOpenApiSpec(specPath: string, baseUrl: string | undefined): P
}
}

function loadPackageVersion(): string {
const packageJsonPath = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', 'package.json')

try {
const parsed = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')) as { version?: unknown }
if (typeof parsed.version === 'string') {
return parsed.version
}
} catch (error) {
console.warn('Failed to read package version:', (error as Error).message)
}

return '1.0.0'
}

export async function initProxy(specPath: string, baseUrl: string |undefined) {
const openApiSpec = await loadOpenApiSpec(specPath, baseUrl)
const proxy = new MCPProxy('Notion API', openApiSpec)
const proxy = new MCPProxy('Notion API', openApiSpec, loadPackageVersion())

return proxy
}
12 changes: 12 additions & 0 deletions src/openapi-mcp-server/mcp/__tests__/proxy.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MCPProxy } from '../proxy'
import { OpenAPIV3 } from 'openapi-types'
import { HttpClient } from '../../client/http-client'
import { Server } from '@modelcontextprotocol/sdk/server/index.js'
import type { Transport } from '@modelcontextprotocol/sdk/shared/transport.js'
import { describe, expect, it, beforeEach, afterEach, vi } from 'vitest'

Expand Down Expand Up @@ -41,6 +42,17 @@ describe('MCPProxy', () => {
proxy = new MCPProxy('test-proxy', mockOpenApiSpec)
})

it('should pass the configured server version to the MCP SDK server', () => {
vi.clearAllMocks()

proxy = new MCPProxy('test-proxy', mockOpenApiSpec, '2.3.1')

expect(Server).toHaveBeenCalledWith(
{ name: 'test-proxy', version: '2.3.1' },
{ capabilities: { tools: {} } },
)
})

describe('listTools handler', () => {
it('should return converted tools from OpenAPI spec', async () => {
const server = (proxy as any).server
Expand Down
4 changes: 2 additions & 2 deletions src/openapi-mcp-server/mcp/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ export class MCPProxy {
private tools: Record<string, NewToolDefinition>
private openApiLookup: Record<string, OpenAPIV3.OperationObject & { method: string; path: string }>

constructor(name: string, openApiSpec: OpenAPIV3.Document) {
this.server = new Server({ name, version: '1.0.0' }, { capabilities: { tools: {} } })
constructor(name: string, openApiSpec: OpenAPIV3.Document, version = '1.0.0') {
this.server = new Server({ name, version }, { capabilities: { tools: {} } })
const baseUrl = openApiSpec.servers?.[0].url
if (!baseUrl) {
throw new Error('No base URL found in OpenAPI spec')
Expand Down