Skip to content

Commit 0cef77d

Browse files
authored
Merge pull request #1 from opencontextprotocol/feat/parser-registry-pattern
feat!: refactor tool discovery into extensible parser registry pattern
2 parents 0c0c459 + e791ce6 commit 0cef77d

11 files changed

Lines changed: 1545 additions & 1278 deletions

src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ export {
5959
type OCPAPISpec
6060
} from './schema_discovery.js';
6161

62+
// Parser system
63+
export {
64+
APISpecParser,
65+
ParserRegistry,
66+
OpenAPIParser
67+
} from './parsers/index.js';
68+
6269
// Registry
6370
export { OCPRegistry } from './registry.js';
6471

src/parsers/base.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Base parser interface and shared types for API specification parsing.
3+
*/
4+
5+
/**
6+
* Tool definition extracted from API specification
7+
*/
8+
export interface OCPTool {
9+
name: string;
10+
description: string;
11+
method: string;
12+
path: string;
13+
parameters: Record<string, any>;
14+
response_schema?: Record<string, any>;
15+
operation_id?: string;
16+
tags?: string[];
17+
}
18+
19+
/**
20+
* Complete API specification with tools
21+
*/
22+
export interface OCPAPISpec {
23+
base_url: string;
24+
title: string;
25+
version: string;
26+
description: string;
27+
tools: OCPTool[];
28+
raw_spec: Record<string, any>;
29+
name?: string;
30+
}
31+
32+
/**
33+
* Abstract base class for API specification parsers.
34+
*
35+
* Implement this interface to add support for new API specification formats
36+
* (e.g., Postman Collections, GraphQL schemas, Google Discovery format).
37+
*/
38+
export abstract class APISpecParser {
39+
/**
40+
* Determine if this parser can handle the given spec data.
41+
*
42+
* @param specData - The raw specification data
43+
* @returns True if this parser can parse the spec
44+
*/
45+
abstract canParse(specData: Record<string, any>): boolean;
46+
47+
/**
48+
* Parse the specification and extract tools.
49+
*
50+
* @param specData - The raw specification data
51+
* @param baseUrlOverride - Optional base URL override
52+
* @param includeResources - Optional resource filter
53+
* @param pathPrefix - Optional path prefix for filtering
54+
* @returns Parsed API specification with tools
55+
*/
56+
abstract parse(
57+
specData: Record<string, any>,
58+
baseUrlOverride?: string,
59+
includeResources?: string[],
60+
pathPrefix?: string
61+
): OCPAPISpec;
62+
63+
/**
64+
* Get the human-readable name of this parser's format.
65+
*
66+
* @returns Format name (e.g., "OpenAPI", "Postman Collection")
67+
*/
68+
abstract getFormatName(): string;
69+
}

src/parsers/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Parser system exports for API specification parsing.
3+
*/
4+
5+
export { APISpecParser, OCPTool, OCPAPISpec } from './base.js';
6+
export { ParserRegistry } from './registry.js';
7+
export { OpenAPIParser } from './openapi_parser.js';

0 commit comments

Comments
 (0)