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
27 changes: 27 additions & 0 deletions src/openapi-mcp-server/mcp/__tests__/proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -836,3 +836,30 @@ describe('MCPProxy', () => {
})
})
})

import { Server } from '@modelcontextprotocol/sdk/server/index.js'
import { getPackageVersion } from '../../../version'

describe('serverInfo version (issue #310)', () => {
let spec: OpenAPIV3.Document

beforeEach(() => {
vi.clearAllMocks()
spec = {
openapi: '3.0.0',
servers: [{ url: 'http://localhost:3000' }],
info: { title: 'Test API', version: '9.9.9' },
paths: {},
}
})

it('reports the package.json version to MCP clients, not the hardcoded 1.0.0', () => {
new MCPProxy('Notion API', spec)
const initArg = (Server as unknown as { mock: { calls: unknown[][] } }).mock.calls.at(-1)![0] as {
name: string
version: string
}
expect(initArg.version).toBe(getPackageVersion())
expect(initArg.version).not.toBe('1.0.0')
})
})
3 changes: 2 additions & 1 deletion src/openapi-mcp-server/mcp/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { OpenAPIToMCPConverter } from '../openapi/parser'
import { HttpClient, HttpClientError } from '../client/http-client'
import { OpenAPIV3 } from 'openapi-types'
import { Transport } from '@modelcontextprotocol/sdk/shared/transport.js'
import { getPackageVersion } from '../../version'

type PathItemObject = OpenAPIV3.PathItemObject & {
get?: OpenAPIV3.OperationObject
Expand Down Expand Up @@ -97,7 +98,7 @@ export class MCPProxy {
* single deployment can serve multiple Notion integrations.
*/
constructor(name: string, openApiSpec: OpenAPIV3.Document, headers?: Record<string, string>) {
this.server = new Server({ name, version: '1.0.0' }, { capabilities: { tools: {} } })
this.server = new Server({ name, version: getPackageVersion() }, { capabilities: { tools: {} } })
const baseUrl = openApiSpec.servers?.[0].url
if (!baseUrl) {
throw new Error('No base URL found in OpenAPI spec')
Expand Down
38 changes: 38 additions & 0 deletions src/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { readFileSync } from 'node:fs'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'

/**
* Resolve this package's version by walking up from the current module to the
* nearest package.json. This is robust across `tsx`/source runs, the compiled
* `build/` output, and installation under `node_modules` — each resolves to the
* correct package.json regardless of how deep the running file is nested.
*
* Replaces the previously hardcoded MCP `serverInfo.version` of `1.0.0`.
* @see https://github.com/makenotion/notion-mcp-server/issues/310
*/
let cached: string | undefined

export function getPackageVersion(): string {
if (cached) return cached
let dir = dirname(fileURLToPath(import.meta.url))
for (let i = 0; i < 8; i++) {
try {
const pkg = JSON.parse(readFileSync(join(dir, 'package.json'), 'utf-8')) as {
name?: string
version?: string
}
if (pkg && typeof pkg.version === 'string' && pkg.name) {
cached = pkg.version
return cached
}
} catch {
// no package.json at this level — keep walking up
}
const parent = dirname(dir)
if (parent === dir) break
dir = parent
}
cached = '0.0.0'
return cached
}