microsoft--mcp-for-beginners
305 行
12 KiB
Markdown
305 行
12 KiB
Markdown
# Enterprise Integration
|
|
|
|
When building MCP Servers in an enterprise context, you often need to integrate with existing AI platforms and services. This section covers how to integrate MCP with enterprise systems like Azure OpenAI and Microsoft AI Foundry, enabling advanced AI capabilities and tool orchestration.
|
|
|
|
## Introduction
|
|
|
|
In this lesson, you'll learn how to integrate Model Context Protocol (MCP) with enterprise AI systems, focusing on Azure OpenAI and Microsoft AI Foundry. These integrations allow you to leverage powerful AI models and tools while maintaining the flexibility and extensibility of MCP.
|
|
|
|
## Learning Objectives
|
|
|
|
By the end of this lesson, you will be able to:
|
|
|
|
- Integrate MCP with Azure OpenAI to utilize its AI capabilities.
|
|
- Implement MCP tool orchestration with Azure OpenAI.
|
|
- Combine MCP with Microsoft AI Foundry for advanced AI agent capabilities.
|
|
- Leverage Azure Machine Learning (ML) for executing ML pipelines and registering models as MCP tools.
|
|
|
|
## Azure OpenAI Integration
|
|
|
|
Azure OpenAI provides access to powerful AI models like GPT-4 and others. Integrating MCP with Azure OpenAI allows you to utilize these models while maintaining the flexibility of MCP's tool orchestration.
|
|
|
|
### C# Implementation
|
|
|
|
In this code snippet, we demonstrate how to integrate MCP with Azure OpenAI using the Azure OpenAI SDK.
|
|
|
|
```csharp
|
|
// .NET Azure OpenAI Integration
|
|
using Microsoft.Mcp.Client;
|
|
using Azure.AI.OpenAI;
|
|
using Microsoft.Extensions.Configuration;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace EnterpriseIntegration
|
|
{
|
|
public class AzureOpenAiMcpClient
|
|
{
|
|
private readonly string _endpoint;
|
|
private readonly string _apiKey;
|
|
private readonly string _deploymentName;
|
|
|
|
public AzureOpenAiMcpClient(IConfiguration config)
|
|
{
|
|
_endpoint = config["AzureOpenAI:Endpoint"];
|
|
_apiKey = config["AzureOpenAI:ApiKey"];
|
|
_deploymentName = config["AzureOpenAI:DeploymentName"];
|
|
}
|
|
|
|
public async Task<string> GetCompletionWithToolsAsync(string prompt, params string[] allowedTools)
|
|
{
|
|
// Create OpenAI client
|
|
var client = new OpenAIClient(new Uri(_endpoint), new AzureKeyCredential(_apiKey));
|
|
|
|
// Create completion options with tools
|
|
var completionOptions = new ChatCompletionsOptions
|
|
{
|
|
DeploymentName = _deploymentName,
|
|
Messages = { new ChatMessage(ChatRole.User, prompt) },
|
|
Temperature = 0.7f,
|
|
MaxTokens = 800
|
|
};
|
|
|
|
// Add tool definitions
|
|
foreach (var tool in allowedTools)
|
|
{
|
|
completionOptions.Tools.Add(new ChatCompletionsFunctionToolDefinition
|
|
{
|
|
Name = tool,
|
|
// In a real implementation, you'd add the tool schema here
|
|
});
|
|
}
|
|
|
|
// Get completion response
|
|
var response = await client.GetChatCompletionsAsync(completionOptions);
|
|
|
|
// Handle tool calls in the response
|
|
foreach (var toolCall in response.Value.Choices[0].Message.ToolCalls)
|
|
{
|
|
// Implementation to handle Azure OpenAI tool calls with MCP
|
|
// ...
|
|
}
|
|
|
|
return response.Value.Choices[0].Message.Content;
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
In the preceding code we've:
|
|
|
|
- Configured the Azure OpenAI client with the endpoint, deployment name and API key.
|
|
- Created a method `GetCompletionWithToolsAsync` to get completions with tool support.
|
|
- Handled tool calls in the response.
|
|
|
|
You're encouraged to implement the actual tool handling logic based on your specific MCP server setup.
|
|
|
|
## Microsoft Foundry Integration
|
|
|
|
Microsoft Foundry provides a platform for building and deploying AI agents. Integrating MCP with Microsoft Foundry allows you to leverage its capabilities while maintaining the flexibility of MCP.
|
|
|
|
In the below code, we develop an Agent integration that processes requests and handles tool calls using MCP.
|
|
|
|
### Java Implementation
|
|
|
|
```java
|
|
// Java AI Foundry Agent Integration
|
|
package com.example.mcp.enterprise;
|
|
|
|
import com.microsoft.aifoundry.AgentClient;
|
|
import com.microsoft.aifoundry.AgentToolResponse;
|
|
import com.microsoft.aifoundry.models.AgentRequest;
|
|
import com.microsoft.aifoundry.models.AgentResponse;
|
|
import com.mcp.client.McpClient;
|
|
import com.mcp.tools.ToolRequest;
|
|
import com.mcp.tools.ToolResponse;
|
|
|
|
public class AIFoundryMcpBridge {
|
|
private final AgentClient agentClient;
|
|
private final McpClient mcpClient;
|
|
|
|
public AIFoundryMcpBridge(String aiFoundryEndpoint, String mcpServerUrl) {
|
|
this.agentClient = new AgentClient(aiFoundryEndpoint);
|
|
this.mcpClient = new McpClient.Builder()
|
|
.setServerUrl(mcpServerUrl)
|
|
.build();
|
|
}
|
|
|
|
public AgentResponse processAgentRequest(AgentRequest request) {
|
|
// Process the AI Foundry Agent request
|
|
AgentResponse initialResponse = agentClient.processRequest(request);
|
|
|
|
// Check if the agent requested to use tools
|
|
if (initialResponse.getToolCalls() != null && !initialResponse.getToolCalls().isEmpty()) {
|
|
// For each tool call, route it to the appropriate MCP tool
|
|
for (AgentToolCall toolCall : initialResponse.getToolCalls()) {
|
|
String toolName = toolCall.getName();
|
|
Map<String, Object> parameters = toolCall.getArguments();
|
|
|
|
// Execute the tool using MCP
|
|
ToolResponse mcpResponse = mcpClient.executeTool(toolName, parameters);
|
|
|
|
// Create tool response for AI Foundry
|
|
AgentToolResponse toolResponse = new AgentToolResponse(
|
|
toolCall.getId(),
|
|
mcpResponse.getResult()
|
|
);
|
|
|
|
// Submit tool response back to the agent
|
|
initialResponse = agentClient.submitToolResponse(
|
|
request.getConversationId(),
|
|
toolResponse
|
|
);
|
|
}
|
|
}
|
|
|
|
return initialResponse;
|
|
}
|
|
}
|
|
```
|
|
|
|
In the preceding code, we've:
|
|
|
|
- Created an `AIFoundryMcpBridge` class that integrates with both AI Foundry and MCP.
|
|
- Implemented a method `processAgentRequest` that processes an AI Foundry agent request.
|
|
- Handled tool calls by executing them through the MCP client and submitting the results back to the AI Foundry agent.
|
|
|
|
## Integrating MCP with Azure ML
|
|
|
|
Integrating MCP with Azure Machine Learning (ML) allows you to leverage Azure's powerful ML capabilities while maintaining the flexibility of MCP. This integration can be used to execute ML pipelines, register models as tools, and manage compute resources.
|
|
|
|
### Python Implementation
|
|
|
|
```python
|
|
# Python Azure AI Integration
|
|
from mcp_client import McpClient
|
|
from azure.ai.ml import MLClient
|
|
from azure.identity import DefaultAzureCredential
|
|
from azure.ai.ml.entities import Environment, AmlCompute
|
|
import os
|
|
import asyncio
|
|
|
|
class EnterpriseAiIntegration:
|
|
def __init__(self, mcp_server_url, subscription_id, resource_group, workspace_name):
|
|
# Set up MCP client
|
|
self.mcp_client = McpClient(server_url=mcp_server_url)
|
|
|
|
# Set up Azure ML client
|
|
self.credential = DefaultAzureCredential()
|
|
self.ml_client = MLClient(
|
|
self.credential,
|
|
subscription_id,
|
|
resource_group,
|
|
workspace_name
|
|
)
|
|
|
|
async def execute_ml_pipeline(self, pipeline_name, input_data):
|
|
"""Executes an ML pipeline in Azure ML"""
|
|
# First process the input data using MCP tools
|
|
processed_data = await self.mcp_client.execute_tool(
|
|
"dataPreprocessor",
|
|
{
|
|
"data": input_data,
|
|
"operations": ["normalize", "clean", "transform"]
|
|
}
|
|
)
|
|
|
|
# Submit the pipeline to Azure ML
|
|
pipeline_job = self.ml_client.jobs.create_or_update(
|
|
entity={
|
|
"name": pipeline_name,
|
|
"display_name": f"MCP-triggered {pipeline_name}",
|
|
"experiment_name": "mcp-integration",
|
|
"inputs": {
|
|
"processed_data": processed_data.result
|
|
}
|
|
}
|
|
)
|
|
|
|
# Return job information
|
|
return {
|
|
"job_id": pipeline_job.id,
|
|
"status": pipeline_job.status,
|
|
"creation_time": pipeline_job.creation_context.created_at
|
|
}
|
|
|
|
async def register_ml_model_as_tool(self, model_name, model_version="latest"):
|
|
"""Registers an Azure ML model as an MCP tool"""
|
|
# Get model details
|
|
if model_version == "latest":
|
|
model = self.ml_client.models.get(name=model_name, label="latest")
|
|
else:
|
|
model = self.ml_client.models.get(name=model_name, version=model_version)
|
|
|
|
# Create deployment environment
|
|
env = Environment(
|
|
name="mcp-model-env",
|
|
conda_file="./environments/inference-env.yml"
|
|
)
|
|
|
|
# Set up compute
|
|
compute = self.ml_client.compute.get("mcp-inference")
|
|
|
|
# Deploy model as online endpoint
|
|
deployment = self.ml_client.online_deployments.create_or_update(
|
|
endpoint_name=f"mcp-{model_name}",
|
|
deployment={
|
|
"name": f"mcp-{model_name}-deployment",
|
|
"model": model.id,
|
|
"environment": env,
|
|
"compute": compute,
|
|
"scale_settings": {
|
|
"scale_type": "auto",
|
|
"min_instances": 1,
|
|
"max_instances": 3
|
|
}
|
|
}
|
|
)
|
|
|
|
# Create MCP tool schema based on model schema
|
|
tool_schema = {
|
|
"type": "object",
|
|
"properties": {},
|
|
"required": []
|
|
}
|
|
|
|
# Add input properties based on model schema
|
|
for input_name, input_spec in model.signature.inputs.items():
|
|
tool_schema["properties"][input_name] = {
|
|
"type": self._map_ml_type_to_json_type(input_spec.type)
|
|
}
|
|
tool_schema["required"].append(input_name)
|
|
|
|
# Register as MCP tool
|
|
# In a real implementation, you would create a tool that calls the endpoint
|
|
return {
|
|
"model_name": model_name,
|
|
"model_version": model.version,
|
|
"endpoint": deployment.endpoint_uri,
|
|
"tool_schema": tool_schema
|
|
}
|
|
|
|
def _map_ml_type_to_json_type(self, ml_type):
|
|
"""Maps ML data types to JSON schema types"""
|
|
mapping = {
|
|
"float": "number",
|
|
"int": "integer",
|
|
"bool": "boolean",
|
|
"str": "string",
|
|
"object": "object",
|
|
"array": "array"
|
|
}
|
|
return mapping.get(ml_type, "string")
|
|
```
|
|
|
|
In the preceding code, we've:
|
|
|
|
- Created an `EnterpriseAiIntegration` class that integrates MCP with Azure ML.
|
|
- Implemented an `execute_ml_pipeline` method that processes input data using MCP tools and submits an ML pipeline to Azure ML.
|
|
- Implemented a `register_ml_model_as_tool` method that registers an Azure ML model as an MCP tool, including creating the necessary deployment environment and compute resources.
|
|
- Mapped Azure ML data types to JSON schema types for tool registration.
|
|
- Used asynchronous programming to handle potentially long-running operations like ML pipeline execution and model registration.
|
|
|
|
## What's next
|
|
|
|
- [5.2 Multi modality](../mcp-multi-modality/README.md)
|