Skip to content

Commit

Permalink
added MCP formatted open API spec
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso committed Feb 23, 2025
1 parent 780a16d commit 9f38ac9
Show file tree
Hide file tree
Showing 2 changed files with 318 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/scripts/autodoc.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import crypto from 'crypto'
import fs from 'fs'
import path from 'path'

Expand All @@ -12,13 +13,13 @@ import {
} from '@/formulas/formulas'
import { Formula, FormulaType, NestedFormulaMap } from '@/types'

const OPENAPI_PATH = path.join(__dirname, '../../static/openapi.json')

const program = new Command('autodoc')
program.description('Autogenerate OpenAPI spec from the available formulas.')

const OPENAPI_BASE_PATH = path.join(__dirname, '../../static/openapi.json')

const openapi: OpenAPIV3_1.Document = JSON.parse(
fs.readFileSync(OPENAPI_PATH, 'utf8')
fs.readFileSync(OPENAPI_BASE_PATH, 'utf8')
)

openapi.tags = [
Expand Down Expand Up @@ -74,13 +75,22 @@ const makeFormulaDoc = (
): [string, OpenAPIV3_1.PathItemObject] => {
const hasAddress = type !== FormulaType.Generic

// tools must follow the regex: ^[a-zA-Z0-9_-]{1,64}$
// so slice to 48 characters, hash it, and add an 8-character suffix
const base = path.replace(/\//g, '_').slice(0, 48)
const operationId =
base +
'_' +
crypto.createHash('sha256').update(base).digest('hex').slice(0, 7)

return [
`/{chainId}/${type}/${hasAddress ? '{address}' : '_'}/${path}`,
{
get: {
tags: [type],
summary: path.split('/').join(' > '),
summary: path.replace(/\//g, ' > '),
description: formula.docs?.description || '`' + path + '`',
operationId,
parameters: [
{
name: 'chainId',
Expand Down Expand Up @@ -151,4 +161,4 @@ openapi.paths = {
),
}

fs.writeFileSync(OPENAPI_PATH, JSON.stringify(openapi, null, 2))
fs.writeFileSync(OPENAPI_BASE_PATH, JSON.stringify(openapi, null, 2))
Loading

0 comments on commit 9f38ac9

Please sign in to comment.