Skip to content

Commit a7e26f5

Browse files
authored
Merge branch 'main' into add-hybrid-search-sample
2 parents 54bf920 + f4495d9 commit a7e26f5

File tree

9 files changed

+137
-11
lines changed

9 files changed

+137
-11
lines changed

dotnet/Directory.Packages.props

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
<PackageVersion Include="Aspire.Hosting.Azure.Search" Version="9.1.0" />
1010
<PackageVersion Include="AWSSDK.BedrockAgent" Version="4.0.0-preview.10" />
1111
<PackageVersion Include="AWSSDK.BedrockAgentRuntime" Version="4.0.0-preview.10" />
12-
<PackageVersion Include="AWSSDK.BedrockRuntime" Version="4.0.0-preview.10" />
13-
<PackageVersion Include="AWSSDK.Core" Version="4.0.0-preview.10" />
12+
<PackageVersion Include="AWSSDK.BedrockRuntime" Version="4.0.0-preview.11" />
13+
<PackageVersion Include="AWSSDK.Core" Version="4.0.0-preview.11" />
1414
<PackageVersion Include="Aspire.Azure.AI.OpenAI" Version="9.1.0-preview.1.25121.10" />
1515
<PackageVersion Include="Aspire.Hosting.AppHost" Version="9.1.0" />
1616
<PackageVersion Include="Aspire.Hosting.Azure.CognitiveServices" Version="9.1.0" />
@@ -29,7 +29,7 @@
2929
<PackageVersion Include="EntityFramework" Version="6.5.1" />
3030
<PackageVersion Include="FastBertTokenizer" Version="1.0.28" />
3131
<PackageVersion Include="Google.Apis.Auth" Version="1.69.0" />
32-
<PackageVersion Include="ModelContextProtocol" Version="0.1.0-preview.1.25171.12" />
32+
<PackageVersion Include="ModelContextProtocol" Version="0.1.0-preview.4" />
3333
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.13" />
3434
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="8.0.14" />
3535
<PackageVersion Include="Microsoft.ML.Tokenizers.Data.Cl100kBase" Version="1.0.1" />

dotnet/samples/Demos/ModelContextProtocolClientServer/MCPClient/Program.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
using Microsoft.Extensions.Logging;
1010
using Microsoft.SemanticKernel;
1111
using Microsoft.SemanticKernel.Connectors.OpenAI;
12+
using ModelContextProtocol;
1213
using ModelContextProtocol.Client;
13-
using ModelContextProtocol.Configuration;
1414
using ModelContextProtocol.Protocol.Transport;
1515

1616
namespace MCPClient;
@@ -52,7 +52,7 @@ public static async Task Main(string[] args)
5252

5353
// Retrieve and display the list of tools available on the MCP server
5454
Console.WriteLine("Available MCP tools:");
55-
var tools = await mcpClient.GetAIFunctionsAsync().ConfigureAwait(false);
55+
var tools = await mcpClient.ListToolsAsync().ConfigureAwait(false);
5656
foreach (var tool in tools)
5757
{
5858
Console.WriteLine($"{tool.Name}: {tool.Description}");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using Microsoft.SemanticKernel;
4+
using ModelContextProtocol.Server;
5+
6+
namespace MCPServer;
7+
8+
/// <summary>
9+
/// Extension methods for <see cref="IMcpServerBuilder"/>.
10+
/// </summary>
11+
public static class McpServerBuilderExtensions
12+
{
13+
/// <summary>
14+
/// Adds all functions of the kernel plugins as tools to the server.
15+
/// </summary>
16+
/// <param name="builder">The builder instance.</param>
17+
/// <param name="plugins">The kernel plugins to add as tools to the server.</param>
18+
/// <returns>The builder instance.</returns>
19+
public static IMcpServerBuilder WithTools(this IMcpServerBuilder builder, KernelPluginCollection plugins)
20+
{
21+
foreach (var plugin in plugins)
22+
{
23+
foreach (var function in plugin)
24+
{
25+
builder.Services.AddSingleton(services => McpServerTool.Create(function.AsAIFunction()));
26+
}
27+
}
28+
29+
return builder;
30+
}
31+
}

dotnet/samples/Demos/ModelContextProtocolClientServer/MCPServer/Program.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

3+
using MCPServer;
34
using MCPServer.Tools;
45
using Microsoft.SemanticKernel;
5-
using ModelContextProtocol;
66

77
// Create a kernel builder and add plugins
88
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
@@ -16,6 +16,6 @@
1616
builder.Services
1717
.AddMcpServer()
1818
.WithStdioServerTransport()
19-
// Add kernel functions to the MCP server as MCP tools
20-
.WithTools(kernel.Plugins.SelectMany(p => p.Select(f => f.AsAIFunction())));
19+
// Add all functions from the kernel plugins to the MCP server as tools
20+
.WithTools(kernel.Plugins);
2121
await builder.Build().RunAsync();

dotnet/samples/Demos/ModelContextProtocolPlugin/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
new() { ClientInfo = new() { Name = "GitHub", Version = "1.0.0" } }).ConfigureAwait(false);
3535

3636
// Retrieve the list of tools available on the GitHub server
37-
var tools = await mcpClient.GetAIFunctionsAsync().ConfigureAwait(false);
37+
var tools = await mcpClient.ListToolsAsync().ConfigureAwait(false);
3838
foreach (var tool in tools)
3939
{
4040
Console.WriteLine($"{tool.Name}: {tool.Description}");

dotnet/src/Agents/Abstractions/Agents.Abstractions.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
<RootNamespace>Microsoft.SemanticKernel.Agents</RootNamespace>
77
<TargetFrameworks>net8.0;netstandard2.0</TargetFrameworks>
88
<EnablePackageValidation>false</EnablePackageValidation>
9-
<VersionSuffix>preview</VersionSuffix>
109
</PropertyGroup>
1110

1211
<Import Project="$(RepoRoot)/dotnet/nuget/nuget-package.props" />

dotnet/src/Agents/Core/Agents.Core.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
<TargetFrameworks>net8.0;netstandard2.0</TargetFrameworks>
88
<NoWarn>$(NoWarn);SKEXP0110;SKEXP0001</NoWarn>
99
<EnablePackageValidation>false</EnablePackageValidation>
10-
<VersionSuffix>preview</VersionSuffix>
1110
</PropertyGroup>
1211

1312
<Import Project="$(RepoRoot)/dotnet/nuget/nuget-package.props" />

python/samples/concepts/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- [Azure AI Agent Prompt Templating](./agents/azure_ai_agent/azure_ai_agent_prompt_templating.py)
1313
- [Azure AI Agent Chat History Callback](./agents/azure_ai_agent/azure_ai_agent_streaming_chat_history_callback.py)
1414
- [Azure AI Agent Streaming](./agents/azure_ai_agent/azure_ai_agent_streaming.py)
15+
- [Azure AI Agent Structured Outputs](./agents/azure_ai_agent/azure_ai_agent_structured_outputs.py)
1516

1617
#### [Bedrock Agent](../../semantic_kernel/agents/bedrock/bedrock_agent.py)
1718

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Copyright (c) Microsoft. All rights reserved.
2+
3+
import asyncio
4+
from enum import Enum
5+
6+
from azure.ai.projects.models import (
7+
ResponseFormatJsonSchema,
8+
ResponseFormatJsonSchemaType,
9+
)
10+
from azure.identity.aio import DefaultAzureCredential
11+
from pydantic import BaseModel
12+
13+
from semantic_kernel.agents import (
14+
AzureAIAgent,
15+
AzureAIAgentSettings,
16+
)
17+
18+
"""
19+
The following sample demonstrates how to create an Azure AI Agent
20+
and leverage the agent's ability to return structured outputs,
21+
based on a user-defined Pydantic model.
22+
"""
23+
24+
25+
# Define a Pydantic model that represents the structured output from the agent
26+
class Planets(str, Enum):
27+
Earth = "Earth"
28+
Mars = "Mars"
29+
Jupyter = "Jupyter"
30+
31+
32+
class Planet(BaseModel):
33+
planet: Planets
34+
mass: float
35+
36+
37+
async def main():
38+
ai_agent_settings = AzureAIAgentSettings.create()
39+
async with (
40+
DefaultAzureCredential() as creds,
41+
AzureAIAgent.create_client(
42+
credential=creds,
43+
conn_str=ai_agent_settings.project_connection_string.get_secret_value(),
44+
) as client,
45+
):
46+
# Create the agent definition
47+
agent_definition = await client.agents.create_agent(
48+
model=ai_agent_settings.model_deployment_name,
49+
name="Assistant",
50+
instructions="Extract the information about planets.",
51+
response_format=ResponseFormatJsonSchemaType(
52+
json_schema=ResponseFormatJsonSchema(
53+
name="planet_mass",
54+
description="Extract planet mass.",
55+
schema=Planet.model_json_schema(),
56+
)
57+
),
58+
)
59+
60+
# Create the AzureAI Agent
61+
agent = AzureAIAgent(
62+
client=client,
63+
definition=agent_definition,
64+
)
65+
66+
# Create a new thread for use with the assistant
67+
# If no thread is provided, a new thread will be
68+
# created and returned with the initial response
69+
thread = None
70+
71+
user_inputs = ["The mass of the Mars is 6.4171E23 kg; the mass of the Earth is 5.972168E24 kg;"]
72+
73+
try:
74+
for user_input in user_inputs:
75+
print(f"# User: '{user_input}'")
76+
async for response in agent.invoke(messages=user_input, thread=thread):
77+
# The response returned is a Pydantic Model, so we can validate it using the
78+
# model_validate_json method
79+
response_model = Planet.model_validate_json(str(response.content))
80+
print(f"# {response.role}: {response_model}")
81+
thread = response.thread
82+
finally:
83+
await thread.delete() if thread else None
84+
await client.agents.delete_agent(agent_definition.id)
85+
86+
"""
87+
Sample Output:
88+
89+
# User: 'The mass of the Mars is 6.4171E23 kg; the mass of the Earth is 5.972168E24 kg;'
90+
# AuthorRole.ASSISTANT: planet=<Planets.Earth: 'Earth'> mass=5.972168e+24
91+
# AuthorRole.ASSISTANT: planet=<Planets.Mars: 'Mars'> mass=6.4171e+23
92+
"""
93+
94+
95+
if __name__ == "__main__":
96+
asyncio.run(main())

0 commit comments

Comments
 (0)