Use max_completion_tokens instead of deprecated max_tokens in OpenAI handler (#3254)

🤖 Generated with Claude Code

Co-authored-by: Marvin Context Protocol <41898282+Marvin Context Protocol@users.noreply.github.com>
Co-authored-by: Jeremiah Lowin <jlowin@users.noreply.github.com>
This commit is contained in:
Jeremiah Lowin 2026-02-20 19:31:38 -05:00 committed by GitHub
commit 2218a6f52a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 36 additions and 2 deletions

View file

@ -84,7 +84,7 @@ class OpenAISamplingHandler:
kwargs: dict[str, Any] = {
"model": model,
"messages": openai_messages,
"max_tokens": params.maxTokens,
"max_completion_tokens": params.maxTokens,
}
if params.temperature is not None:
kwargs["temperature"] = params.temperature

View file

@ -1,7 +1,8 @@
from unittest.mock import MagicMock
from unittest.mock import AsyncMock, MagicMock
import pytest
from mcp.types import (
CreateMessageRequestParams,
CreateMessageResult,
ModelHint,
ModelPreferences,
@ -72,6 +73,39 @@ def test_select_model_from_preferences(prefs, expected):
assert handler._select_model_from_preferences(prefs) == expected
async def test_handler_passes_max_completion_tokens():
"""Verify the handler uses max_completion_tokens (not max_tokens)."""
mock_client = MagicMock(spec=AsyncOpenAI)
mock_client.chat = MagicMock()
mock_client.chat.completions = MagicMock()
mock_client.chat.completions.create = AsyncMock(
return_value=ChatCompletion(
id="123",
created=123,
model="gpt-4o-mini",
object="chat.completion",
choices=[
Choice(
message=ChatCompletionMessage(content="hi", role="assistant"),
finish_reason="stop",
index=0,
)
],
)
)
handler = OpenAISamplingHandler(default_model="gpt-4o-mini", client=mock_client)
messages = [
SamplingMessage(role="user", content=TextContent(type="text", text="hello"))
]
params = CreateMessageRequestParams(messages=messages, maxTokens=300)
await handler(messages, params, context=None) # type: ignore[arg-type]
call_kwargs = mock_client.chat.completions.create.call_args
assert "max_completion_tokens" in call_kwargs.kwargs
assert call_kwargs.kwargs["max_completion_tokens"] == 300
assert "max_tokens" not in call_kwargs.kwargs
async def test_chat_completion_to_create_message_result():
mock_client = MagicMock(spec=AsyncOpenAI)
handler = OpenAISamplingHandler(default_model="fallback-model", client=mock_client) # type: ignore[arg-type]