-
Notifications
You must be signed in to change notification settings - Fork 172
feat: add hosted mcp tool support #30
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
Closed
+5,538
−6,649
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Hosted Model Context Protocol Example | ||
|
||
This example demonstrates how to use [OpenAI Hosted MCP](https://platform.openai.com/docs/guides/tools-remote-mcp) servers with the OpenAI Agents SDK. | ||
|
||
The example shows two different scenarios: | ||
|
||
- `simple.ts` - A basic example of connecting to a hosted MCP server with no approval required. | ||
- `approvals.ts` - An example showing how to use an approvals workflow with a hosted MCP server. | ||
|
||
Run the examples from the repository root: | ||
|
||
### Simple | ||
|
||
```bash | ||
pnpm -F hosted-mcp start:simple | ||
``` | ||
|
||
### Approvals | ||
|
||
```bash | ||
pnpm -F hosted-mcp start:approvals | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { Agent, run, hostedMCPTool } from '@openai/agents'; | ||
import * as readline from 'readline'; | ||
|
||
/** | ||
* This example demonstrates how to use the hosted MCP support in the OpenAI Responses API, with | ||
* approval callbacks. | ||
*/ | ||
|
||
// Create readline interface for user input | ||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
}); | ||
|
||
function approvalCallback(toolName: string): Promise<boolean> { | ||
return new Promise((resolve) => { | ||
rl.question( | ||
`Approve running the tool \`${toolName}\`? (y/n) `, | ||
(answer) => { | ||
const approved = answer.toLowerCase() === 'y'; | ||
if (!approved) { | ||
console.log('User denied'); | ||
} | ||
resolve(approved); | ||
}, | ||
); | ||
}); | ||
} | ||
|
||
async function main(verbose: boolean, stream: boolean) { | ||
const agent = new Agent({ | ||
name: 'Assistant', | ||
tools: [ | ||
hostedMCPTool({ | ||
serverLabel: 'gitmcp', | ||
serverUrl: 'https://gitmcp.io/openai/codex', | ||
requireApproval: 'always', | ||
}), | ||
], | ||
}); | ||
|
||
let result: any; | ||
|
||
if (stream) { | ||
result = await run(agent, 'Which language is this repo written in?', { | ||
stream: true, | ||
}); | ||
|
||
for await (const event of result.toStream()) { | ||
if (event.type === 'run_item_stream_event') { | ||
console.log(`Got event of type ${event.item.constructor.name}`); | ||
} | ||
} | ||
console.log(`Done streaming; final result: ${result.finalOutput}`); | ||
} else { | ||
result = await run(agent, 'Which language is this repo written in?'); | ||
console.log(result.finalOutput); | ||
} | ||
|
||
if (verbose) { | ||
for (const item of result.newItems) { | ||
console.log(item); | ||
} | ||
} | ||
|
||
// Close readline interface | ||
rl.close(); | ||
} | ||
|
||
// Parse command line arguments | ||
const args = process.argv.slice(2); | ||
const verbose = args.includes('--verbose'); | ||
const stream = args.includes('--stream'); | ||
|
||
main(verbose, stream).catch((error) => { | ||
console.error(error); | ||
rl.close(); | ||
process.exit(1); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"private": true, | ||
"name": "hosted-mcp", | ||
"dependencies": { | ||
"@modelcontextprotocol/sdk": "^1.12.0", | ||
"@openai/agents": "workspace:*" | ||
}, | ||
"scripts": { | ||
"build-check": "tsc --noEmit", | ||
"start:simple": "tsx simple.ts", | ||
"start:approvals": "tsx approvals.ts" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Agent, run, hostedMCPTool } from '@openai/agents'; | ||
|
||
/** | ||
* This example demonstrates how to use the hosted MCP support in the OpenAI Responses API, with | ||
* approvals not required for any tools. You should only use this for trusted MCP servers. | ||
*/ | ||
|
||
async function main(verbose: boolean, stream: boolean) { | ||
const agent = new Agent({ | ||
name: 'Assistant', | ||
tools: [ | ||
hostedMCPTool({ | ||
serverLabel: 'deepwiki', | ||
serverUrl: 'https://mcp.deepwiki.com/mcp', | ||
requireApproval: 'never', | ||
}), | ||
], | ||
}); | ||
|
||
let result: any; | ||
if (stream) { | ||
result = await run( | ||
agent, | ||
'What transport protocols are supported in the 2025-03-26 version of the MCP spec?', | ||
{ | ||
stream: true, | ||
}, | ||
); | ||
for await (const event of result.toStream()) { | ||
if (event.type === 'run_item_stream_event') { | ||
console.log(`Got event of type ${event.item.constructor.name}`); | ||
} else if (event.type === 'run_item_stream_event') { | ||
} | ||
} | ||
console.log(`Done streaming; final result: ${result.finalOutput}`); | ||
} else { | ||
result = await run( | ||
agent, | ||
'What transport protocols are supported in the 2025-03-26 version of the MCP spec?', | ||
); | ||
console.log(result.finalOutput); | ||
// As of the **2025-03-26 version** of the **MCP (Mesh Configuration Protocol) specification**, the following **transport protocols are supported**:... | ||
} | ||
|
||
if (verbose) { | ||
for (const item of result.newItems) { | ||
console.log(item); | ||
} | ||
} | ||
} | ||
|
||
const args = process.argv.slice(2); | ||
const verbose = args.includes('--verbose') || false; | ||
const stream = args.includes('--stream') || false; | ||
|
||
main(verbose, stream).catch(console.error); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "../../tsconfig.examples.json" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,5 @@ export { | |
fileSearchTool, | ||
codeInterpreterTool, | ||
imageGenerationTool, | ||
hostedMCPTool, | ||
} from './tools'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I don't think I understand how this is currently being used