Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 7
feat: introduce fetchTools#114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
a8e62363d210e78916c1edd22858562581aaee92310846baac1d41059ee7d3aa32e6c37ef126d90cdf03b8d71ed3e16bdba73b8d0a30304a8e360614ee7746185e5c89137d63d66998f08d1599File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /** | ||
| * Example: fetch the latest StackOne tool catalog and execute a tool. | ||
| * | ||
| * Set `STACKONE_API_KEY` (and optionally `STACKONE_BASE_URL`) before running. | ||
| * By default the script exits early in test environments where a real key is | ||
| * not available. | ||
| */ | ||
| import process from 'node:process'; | ||
| import { StackOneToolSet } from '../src'; | ||
| const apiKey = process.env.STACKONE_API_KEY; | ||
| const isPlaceholderKey = !apiKey || apiKey === 'test-stackone-key'; | ||
| const shouldSkip = process.env.SKIP_FETCH_TOOLS_EXAMPLE !== '0' && isPlaceholderKey; | ||
| if (shouldSkip) { | ||
| console.log( | ||
| 'Skipping fetch-tools example. Provide STACKONE_API_KEY and set SKIP_FETCH_TOOLS_EXAMPLE=0 to run.' | ||
| ); | ||
| process.exit(0); | ||
| } | ||
| const toolset = new StackOneToolSet({ | ||
| baseUrl: process.env.STACKONE_BASE_URL ?? 'https://api.stackone.com', | ||
| }); | ||
| const tools = await toolset.fetchTools(); | ||
| console.log(`Loaded ${tools.length} tools`); | ||
| const tool = tools.getTool('hris_list_employees'); | ||
| if (!tool) { | ||
| throw new Error('Tool hris_list_employees not found in the catalog'); | ||
| } | ||
| const result = await tool.execute({ | ||
| query: { limit: 5 }, | ||
| }); | ||
| console.log('Sample execution result:', JSON.stringify(result, null, 2)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { Client } from '@modelcontextprotocol/sdk/client/index.js'; | ||
| import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'; | ||
| import { version } from '../package.json'; | ||
| interface MCPClientOptions { | ||
| baseUrl: string; | ||
| headers?: Record<string, string>; | ||
| } | ||
| interface MCPClient { | ||
| /** underlying MCP client */ | ||
| client: Client; | ||
| /** underlying transport */ | ||
| transport: StreamableHTTPClientTransport; | ||
| /** cleanup client and transport */ | ||
| [Symbol.asyncDispose](): Promise<void>; | ||
| } | ||
| /** | ||
| * Create a Model Context Protocol (MCP) client. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * import { createMCPClient } from '@stackone/ai'; | ||
| * | ||
| * await using clients = await createMCPClient({ | ||
| * baseUrl: 'https://api.modelcontextprotocol.org', | ||
| * headers: { | ||
| * 'Authorization': 'Bearer YOUR_API_KEY', | ||
| * }, | ||
| * }); | ||
| * ``` | ||
| */ | ||
| export async function createMCPClient({ baseUrl, headers }: MCPClientOptions): Promise<MCPClient> { | ||
| const transport = new StreamableHTTPClientTransport(new URL(baseUrl), { | ||
| requestInit: { | ||
| headers, | ||
| }, | ||
| }); | ||
| const client = new Client({ | ||
| name: 'StackOne AI SDK', | ||
| version, | ||
| }); | ||
| return { | ||
| client, | ||
| transport, | ||
| async [Symbol.asyncDispose]() { | ||
| await Promise.all([client.close(), transport.close()]); | ||
| }, | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| import type { JSONSchema7 as JsonSchema } from 'json-schema'; | ||
| import type { OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'; | ||
| import { ParameterLocation, type ToolDefinition } from '../types'; | ||
| import { type HttpExecuteConfig, ParameterLocation, type ToolDefinition } from '../types'; | ||
| // Define a type for OpenAPI document | ||
| type OpenAPIDocument = OpenAPIV3.Document | OpenAPIV3_1.Document; | ||
| @@ -67,6 +67,23 @@ export class OpenAPIParser { | ||
| return servers.length > 0 ? servers[0].url : 'https://api.stackone.com'; | ||
| } | ||
| private normalizeBodyType(bodyType: string | null): HttpExecuteConfig['bodyType'] { | ||
| // Map OpenAPI content types into the narrower set supported by ExecuteConfig. | ||
| if (!bodyType) { | ||
| return 'json'; | ||
| } | ||
| if (bodyType === 'form-data' || bodyType === 'multipart-form') { | ||
| return 'multipart-form'; | ||
| } | ||
| if (bodyType === 'form' || bodyType === 'application/x-www-form-urlencoded') { | ||
| return 'form'; | ||
| } | ||
| return 'json'; | ||
ryoppippi marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| /** | ||
| * Create a parser from a JSON string | ||
| * @param specString OpenAPI specification as a JSON string | ||
| @@ -552,27 +569,30 @@ export class OpenAPIParser { | ||
| ); | ||
| // Create tool definition with deep copies to prevent shared state | ||
| const executeConfig = { | ||
| kind: 'http', | ||
| method: method.toUpperCase(), | ||
| url: `${this._baseUrl}${path}`, | ||
| bodyType: this.normalizeBodyType(bodyType), | ||
| params: Object.entries(parameterLocations) | ||
| .filter(([name]) => !this.isRemovedParam(name)) | ||
| .map(([name, location]) => { | ||
| return { | ||
| name, | ||
| location, | ||
| type: (filteredProperties[name]?.type as JsonSchema['type']) || 'string', | ||
| }; | ||
| }), | ||
| } satisfies HttpExecuteConfig; | ||
| tools[name] = { | ||
| description: operation.summary || '', | ||
| parameters: { | ||
| type: 'object', | ||
| properties: filteredProperties, | ||
| required: filteredRequired, | ||
| }, | ||
| execute: { | ||
| method: method.toUpperCase(), | ||
| url: `${this._baseUrl}${path}`, | ||
| bodyType: (bodyType as 'json' | 'multipart-form') || 'json', | ||
| params: Object.entries(parameterLocations) | ||
| .filter(([name]) => !this.isRemovedParam(name)) | ||
| .map(([name, location]) => { | ||
| return { | ||
| name, | ||
| location, | ||
| type: (filteredProperties[name]?.type as JsonSchema['type']) || 'string', | ||
| }; | ||
| }), | ||
| }, | ||
| execute: executeConfig, | ||
| }; | ||
| } catch (operationError) { | ||
| console.error(`Error processing operation ${name}: ${operationError}`); | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
close mcp client with
await using