[FEATURE REQ] Add ResponseTool.CreateMcpTool to configure Remote MCP Servers for Response API
Closed this issue ยท 9 comments
Describe the feature or improvement you are requesting
OpenAI API describes MCP tool type to list Remote MCP when using Response API
In alignment with other tool types, can you please add ResponseTool.CreateMcpTool method.
Example of intended usage:
var azureClient = new AzureOpenAIClient(
new Uri("https://your-openai-endpoint.openai.azure.com/"),
new AzureKeyCredential(Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY")));
var client = azureClient.GetOpenAIResponseClient("your-deployment-name");
OpenAIResponse response = await client.CreateResponseAsync(
userInputText: "What's the optimal strategy to win at poker?",
new ResponseCreationOptions()
{
ReasoningOptions = new ResponseReasoningOptions()
{
ReasoningEffortLevel = ResponseReasoningEffortLevel.High,
},
Tools =
{
ResponseTool.CreateMcpTool("my-mcp-server", "https://example.com/mcp", [{"Authorization", "Bearer: {AUTH_TOKEN}"}])
}
});Additional context
No response
Hi @gophph. Thanks for reaching out and for your inquiry. The mcp tool hasn't been included in the library yet but will be added in a future release. In the meantime, you can access it by using the overload on the client that allows you to drop down to protocol details. This would look something like:
var client = new OpenAIResponseClient("gpt-4.1", "<< YOUR API KEY >>");
// Form the OpenAI payload directly so that we can correct the deprecated member being used.
var rawResponse = await client
.CreateResponseAsync(BinaryContent.Create(BinaryData.FromObjectAsJson(new
{
model = "gpt-4.1",
instructions = "Pretend you are a pirate when answering",
input = "What is the best way to fold a burrito?",
tools = new[]
{
new
{
type = "mcp",
server_label ="my-mcp-server",
server_url = "https://example.com/mcp",
headers = new { Authorization = "Bearer: {AUTH_TOKEN}" }
}
}
})));
// The call returns a raw JSON response. Translate to the associated model so that it is easier to work with.
var response = ModelReaderWriter.Read<OpenAIResponse>(rawResponse.GetRawResponse().Content);
Console.WriteLine($"The model answered:'{response.GetOutputText()}'");Hi!
How to limit client to use specified MCP tools in your example @jsquire?
I mean let's say "https://example.com/mcp" contains 50 different tools but I want to use 2 of them only.
@webwarrior06: From looking at the platform docs, it seems that you could would do this by setting the tool_choice. Assuming that is correct, if I add that to the above sample, it would look something like:
var client = new OpenAIResponseClient("gpt-4.1", "<< YOUR API KEY >>");
// Form the OpenAI payload directly so that we can correct the deprecated member being used.
var rawResponse = await client
.CreateResponseAsync(BinaryContent.Create(BinaryData.FromObjectAsJson(new
{
model = "gpt-4.1",
instructions = "Pretend you are a pirate when answering",
input = "What is the best way to fold a burrito?",
tools = new[]
{
new
{
type = "mcp",
server_label ="my-mcp-server",
server_url = "https://example.com/mcp",
headers = new { Authorization = "Bearer: {AUTH_TOKEN}" }
}
},
tool_choice = new
{
mode = "auto",
type = "allowed_tools",
tools = new[]
{
new { type = "mcp", server_label = "someTool" },
new { type = "mcp", server_label = "otherTool" },
}
}
})));
// The call returns a raw JSON response. Translate to the associated model so that it is easier to work with.
var response = ModelReaderWriter.Read<OpenAIResponse>(rawResponse.GetRawResponse().Content);
Console.WriteLine($"The model answered:'{response.GetOutputText()}'");That said, what you're asking about is service behavior and we are not the authoritative voice - we're inferring from the same docs available to you. For an authoritative answer, you may want to consider reaching out to OpenAI Support Team. You can either email support@openai.com or start a new chat session via the Help Center. The OpenAI support team is available 24/7 and is equipped to assist with the full range of requests.
Thanks for the quick response! Yeah I get it. I will git it a try. This seems like what I am looking for.
When is first-class support for ResponseTool.CreateMcpTool planned?
Dropping down to raw protocol payloads works, but it feels awkward and brittle compared to the other ResponseTool helpers.
Would be great to have an idiomatic API for MCP tools to avoid manually shaping the JSON and losing type safety.
This is planned in the near term, and the initial implementation is out for PR in #656 at present.
This was released with v2.4.0 and is now available. An example of usage can be found in Example05_RemoteMcp.
@jsquire can you please suggest how to instantiate AllowedTools property with list of tools.
Trying following code snippet
ResponseCreationOptions options = new()
{
Tools = {
new McpTool("mcp", new Uri(<redacted>))
{
AllowedTools = new McpToolFilter() { ToolNames = allowedMcpTools.ToList() } ,
ToolCallApprovalPolicy = new McpToolCallApprovalPolicy(GlobalMcpToolCallApprovalPolicy.NeverRequireApproval)
}
}
};
but McpToolFilter.ToolNames is get-only property
@gophph: Please don't leave comments on closed issues unrelated to the original inquiry. A new issue for your question is preferred. As a convention across the OpenAI client library, collection types for models are intentionally read-only. Their value can be set by mutating the existing collection. This can be done by either adding members after the object has been constructed or using C#'s initializer syntax as part of construction.
// Example: initializer syntax
ResponseCreationOptions options = new()
{
Tools =
{
new McpTool("mcp", new Uri(<redacted>))
{
AllowedTools = new McpToolFilter()
{
ToolNames = { "some-tool", "other-tool" }
} ,
ToolCallApprovalPolicy = new McpToolCallApprovalPolicy(GlobalMcpToolCallApprovalPolicy.NeverRequireApproval)
}
}
};
// Example: mutating the collection instance.
McpToolFilter filter = new();
filter.ToolNames.Add("some-tool");
filter.ToolNames.Add("other-tool");
ResponseCreationOptions options = new()
{
Tools =
{
new McpTool("mcp", new Uri(<redacted>))
{
AllowedTools = filter ,
ToolCallApprovalPolicy = new McpToolCallApprovalPolicy(GlobalMcpToolCallApprovalPolicy.NeverRequireApproval)
}
}
};