Repository navigation
ref(agents): Simplify OpenAI provider for gateway compatibility #724
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
128 changes: 128 additions & 0 deletions
128
packages/mcp-core/src/internal/agents/openai-provider.integration.test.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,128 @@ | ||
| /** | ||
| * Integration tests for OpenAI provider | ||
| * | ||
| * These tests verify that the OpenAI provider configuration works correctly | ||
| * with the actual OpenAI API. They require OPENAI_API_KEY to be set. | ||
| * | ||
| * Unlike unit tests (which mock the AI SDK) and evals (which test tool prediction | ||
| * but don't execute handlers), these tests actually call the embedded agents | ||
| * with real OpenAI API calls while mocking Sentry API responses. | ||
| * | ||
| * This catches issues like: | ||
| * - #623: structuredOutputs causing validation errors with nullable fields | ||
| * - 405 errors from unsupported parameters (reasoningEffort, temperature) | ||
| */ | ||
| import { describe, it, expect, beforeAll, afterAll } from "vitest"; | ||
| import { http, HttpResponse } from "msw"; | ||
| import { setupServer } from "msw/node"; | ||
| import { searchIssuesAgent } from "../../tools/search-issues/agent"; | ||
| import { SentryApiService } from "../../api-client"; | ||
| import { setAgentProvider } from "./provider-factory"; | ||
|
|
||
| // Mock Sentry API server - intercepts Sentry calls but bypasses OpenAI | ||
| const mswServer = setupServer( | ||
| // Mock the issue search fields endpoint (called by issueFields tool) | ||
| http.get("*/api/0/organizations/*/issues/", () => { | ||
| return HttpResponse.json([]); | ||
| }), | ||
| // Mock the tags endpoint for field discovery | ||
| http.get("*/api/0/organizations/*/tags/", () => { | ||
| return HttpResponse.json([ | ||
| { key: "environment", name: "Environment" }, | ||
| { key: "level", name: "Level" }, | ||
| { key: "release", name: "Release" }, | ||
| { key: "browser", name: "Browser" }, | ||
| ]); | ||
| }), | ||
| // Mock whoami endpoint (called by whoami tool) | ||
| http.get("*/api/0/users/me/", () => { | ||
| return HttpResponse.json({ | ||
| id: "12345", | ||
| email: "test@example.com", | ||
| name: "Test User", | ||
| }); | ||
| }), | ||
| ); | ||
|
|
||
| describe("OpenAI Provider Integration", () => { | ||
| const hasOpenAIKey = Boolean(process.env.OPENAI_API_KEY); | ||
|
|
||
| beforeAll(() => { | ||
| if (hasOpenAIKey) { | ||
| // Explicitly set OpenAI provider to ensure we test OpenAI even if | ||
| // ANTHROPIC_API_KEY is also set (auto-detect prefers Anthropic) | ||
| setAgentProvider("openai"); | ||
| mswServer.listen({ onUnhandledRequest: "bypass" }); | ||
| } | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| if (hasOpenAIKey) { | ||
| setAgentProvider(undefined); // Reset for other tests | ||
| mswServer.close(); | ||
| } | ||
| }); | ||
|
|
||
| it.skipIf(!hasOpenAIKey)( | ||
| "searchIssuesAgent translates natural language to Sentry query", | ||
| async () => { | ||
| // This tests the actual code path that broke in #623 and with gateway 405s | ||
| // - Uses the real searchIssuesAgent (not mocked) | ||
| // - Calls real OpenAI API | ||
| // - Mocks only the Sentry API responses | ||
|
|
||
| const apiService = new SentryApiService({ | ||
| accessToken: "test-token", | ||
| host: "sentry.io", | ||
| }); | ||
|
|
||
| const { result } = await searchIssuesAgent({ | ||
| query: "unresolved errors in production from the last week", | ||
| organizationSlug: "test-org", | ||
| apiService, | ||
| }); | ||
|
|
||
| // Verify the agent returned a valid structured response | ||
| expect(result).toBeDefined(); | ||
| expect(typeof result.query).toBe("string"); | ||
| expect(typeof result.explanation).toBe("string"); | ||
|
|
||
| // The query should contain relevant Sentry search syntax | ||
| // (exact output varies, but should be parseable) | ||
| expect(result.query.length).toBeGreaterThan(0); | ||
|
|
||
| // sort can be null or one of the valid values | ||
| expect( | ||
| result.sort === null || | ||
| ["date", "freq", "new", "user"].includes(result.sort), | ||
| ).toBe(true); | ||
| }, | ||
| { timeout: 60000 }, | ||
| ); | ||
|
|
||
| it.skipIf(!hasOpenAIKey)( | ||
| "searchIssuesAgent handles nullable sort field correctly", | ||
| async () => { | ||
| // Specifically test that nullable fields work (the #623 issue) | ||
| const apiService = new SentryApiService({ | ||
| accessToken: "test-token", | ||
| host: "sentry.io", | ||
| }); | ||
|
|
||
| const { result } = await searchIssuesAgent({ | ||
| query: "all issues", // Vague query where sort preference is unclear | ||
| organizationSlug: "test-org", | ||
| apiService, | ||
| }); | ||
|
|
||
| // Should succeed without "required field" validation errors | ||
| expect(result).toBeDefined(); | ||
| // sort being null is valid and tests the nullable field handling | ||
| expect( | ||
| result.sort === null || | ||
| ["date", "freq", "new", "user"].includes(result.sort), | ||
| ).toBe(true); | ||
| }, | ||
| { timeout: 60000 }, | ||
| ); | ||
| }); | ||
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.