-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathMcpElements.php
More file actions
152 lines (142 loc) · 5.13 KB
/
Copy pathMcpElements.php
File metadata and controls
152 lines (142 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mcp\Example\Server\OAuthMicrosoft;
use Mcp\Capability\Attribute\McpPrompt;
use Mcp\Capability\Attribute\McpResource;
use Mcp\Capability\Attribute\McpTool;
use Mcp\Server\RequestContext;
/**
* MCP elements for the OAuth Microsoft example.
*
* These tools demonstrate a protected MCP server using Microsoft Entra ID.
* All requests must include a valid Microsoft-issued OAuth bearer token.
*/
final class McpElements
{
/**
* Confirms the user is authenticated with Microsoft.
*
* @return array<string, mixed>
*/
#[McpTool(
name: 'get_auth_status',
description: 'Confirm Microsoft Entra ID authentication status'
)]
public function getAuthStatus(RequestContext $context): array
{
$meta = $context->getRequest()->getMeta() ?? [];
$oauth = isset($meta['oauth']) && \is_array($meta['oauth']) ? $meta['oauth'] : [];
$claims = isset($oauth['oauth.claims']) && \is_array($oauth['oauth.claims']) ? $oauth['oauth.claims'] : [];
$scopes = isset($oauth['oauth.scopes']) && \is_array($oauth['oauth.scopes']) ? $oauth['oauth.scopes'] : [];
return [
'authenticated' => true,
'provider' => 'Microsoft Entra ID',
'message' => 'You have successfully authenticated with Microsoft!',
'timestamp' => date('c'),
'user' => [
'subject' => $oauth['oauth.subject'] ?? ($claims['sub'] ?? null),
'object_id' => $oauth['oauth.object_id'] ?? ($claims['oid'] ?? null),
'username' => $claims['preferred_username'] ?? ($claims['upn'] ?? null),
'name' => $oauth['oauth.name'] ?? ($claims['name'] ?? null),
'email' => $claims['email'] ?? null,
'issuer' => $claims['iss'] ?? null,
'audience' => $claims['aud'] ?? null,
'tenant_id' => $claims['tid'] ?? null,
'scopes' => $scopes,
'expires_at' => isset($claims['exp']) && is_numeric($claims['exp'])
? date('c', (int) $claims['exp'])
: null,
],
];
}
/**
* Simulates calling Microsoft Graph API.
*
* @return array<string, mixed>
*/
#[McpTool(
name: 'call_graph_api',
description: 'Simulate calling Microsoft Graph API'
)]
public function callGraphApi(
string $endpoint = '/me',
): array {
// In a real implementation, you would:
// 1. Use the On-Behalf-Of flow to exchange tokens
// 2. Call Microsoft Graph with the new token
return [
'status' => 'simulated',
'endpoint' => "https://graph.microsoft.com/v1.0{$endpoint}",
'message' => 'Configure AZURE_CLIENT_SECRET for actual Graph API calls',
'simulated_response' => [
'displayName' => 'Demo User',
'mail' => 'demo@example.com',
],
];
}
/**
* Lists simulated emails.
*
* @return array<string, mixed>
*/
#[McpTool(
name: 'list_emails',
description: 'List recent emails (simulated)'
)]
public function listEmails(int $count = 5): array
{
return [
'note' => 'Simulated data. Implement Graph API call with Mail.Read scope for real emails.',
'emails' => array_map(static fn ($i) => [
'id' => 'msg_'.uniqid(),
'subject' => "Sample Email #{$i}",
'from' => "sender{$i}@example.com",
'receivedDateTime' => date('c', strtotime("-{$i} hours")),
], range(1, $count)),
];
}
/**
* Returns the current server status.
*
* @return array<string, mixed>
*/
#[McpResource(
uri: 'server://status',
name: 'server_status',
description: 'Current server status with Microsoft auth info',
mimeType: 'application/json'
)]
public function getServerStatus(): array
{
return [
'status' => 'healthy',
'timestamp' => date('c'),
'auth_provider' => 'Microsoft Entra ID',
'php_version' => \PHP_VERSION,
'memory_usage_mb' => round(memory_get_usage(true) / 1024 / 1024, 2),
];
}
/**
* A Microsoft Teams-style message prompt.
*/
#[McpPrompt(
name: 'teams_message',
description: 'Generate a Microsoft Teams-style message'
)]
public function teamsMessage(string $messageType = 'announcement'): string
{
return match ($messageType) {
'announcement' => "📢 **Announcement**\n\nPlease add your announcement content here.",
'question' => "❓ **Question**\n\nType your question here.",
'update' => "📋 **Status Update**\n\n**Progress:**\n- Item 1\n- Item 2",
default => "💬 **Message**\n\nYour message content here.",
};
}
}