msgraph-mcp-server 0.5.0__tar.gz → 0.5.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/.github/dependabot.yml +6 -0
  2. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/.github/workflows/ci.yml +3 -3
  3. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/.github/workflows/release.yml +2 -2
  4. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/PKG-INFO +3 -2
  5. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/pyproject.toml +9 -2
  6. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/server.json +2 -2
  7. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/src/msgraph_mcp/auth/token.py +7 -2
  8. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/src/msgraph_mcp/config.py +0 -1
  9. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/src/msgraph_mcp/graph/auth_provider.py +0 -1
  10. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/src/msgraph_mcp/graph/batch.py +17 -4
  11. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/src/msgraph_mcp/graph/client.py +3 -2
  12. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/src/msgraph_mcp/graph/errors.py +9 -2
  13. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/src/msgraph_mcp/graph/pagination.py +0 -1
  14. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/src/msgraph_mcp/server.py +5 -6
  15. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/src/msgraph_mcp/tools/__init__.py +2 -2
  16. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/src/msgraph_mcp/tools/_binary.py +3 -3
  17. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/src/msgraph_mcp/tools/calendar.py +9 -10
  18. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/src/msgraph_mcp/tools/mail_folders.py +6 -6
  19. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/src/msgraph_mcp/tools/mail_read.py +20 -6
  20. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/src/msgraph_mcp/tools/mail_rules.py +5 -5
  21. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/src/msgraph_mcp/tools/mail_write.py +7 -8
  22. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/src/msgraph_mcp/tools/teams_channels.py +8 -7
  23. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/src/msgraph_mcp/tools/teams_chats.py +2 -2
  24. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/src/msgraph_mcp/tools/teams_content.py +1 -1
  25. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/src/msgraph_mcp/tools/util.py +1 -1
  26. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/tests/integration/test_live_smoke.py +2 -1
  27. msgraph_mcp_server-0.5.2/tests/sdk_graph.py +38 -0
  28. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/tests/test_graph_batch.py +1 -1
  29. msgraph_mcp_server-0.5.2/tests/test_graph_me_url.py +36 -0
  30. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/tests/test_graph_pagination.py +2 -0
  31. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/tests/test_graph_trimming.py +4 -5
  32. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/tests/test_server.py +4 -5
  33. msgraph_mcp_server-0.5.2/tests/test_tool_errors.py +131 -0
  34. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/tests/test_tools_calendar.py +2 -2
  35. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/tests/test_tools_mail_batch.py +1 -1
  36. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/tests/test_tools_mail_folders.py +2 -2
  37. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/tests/test_tools_mail_read.py +94 -6
  38. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/tests/test_tools_mail_rules.py +9 -9
  39. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/tests/test_tools_teams_content.py +2 -2
  40. msgraph_mcp_server-0.5.2/uv.lock +2004 -0
  41. msgraph_mcp_server-0.5.0/uv.lock +0 -1776
  42. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/.env.example +0 -0
  43. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/.gitignore +0 -0
  44. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/CONTRIBUTING.md +0 -0
  45. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/LICENSE +0 -0
  46. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/README.md +0 -0
  47. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/src/msgraph_mcp/__init__.py +0 -0
  48. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/src/msgraph_mcp/auth/__init__.py +0 -0
  49. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/src/msgraph_mcp/auth/cli.py +0 -0
  50. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/src/msgraph_mcp/auth/msal_app.py +0 -0
  51. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/src/msgraph_mcp/graph/__init__.py +0 -0
  52. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/src/msgraph_mcp/graph/serialize.py +0 -0
  53. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/src/msgraph_mcp/graph/trimming.py +0 -0
  54. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/src/msgraph_mcp/tools/mail_actions.py +0 -0
  55. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/src/msgraph_mcp/tools/mail_batch.py +0 -0
  56. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/tests/__init__.py +0 -0
  57. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/tests/conftest.py +0 -0
  58. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/tests/fixtures/__init__.py +0 -0
  59. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/tests/fixtures/event_minimal.json +0 -0
  60. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/tests/fixtures/message_minimal.json +0 -0
  61. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/tests/integration/__init__.py +0 -0
  62. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/tests/integration/test_rules_and_folders_live.py +0 -0
  63. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/tests/test_auth_cli.py +0 -0
  64. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/tests/test_auth_msal_app.py +0 -0
  65. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/tests/test_auth_token.py +0 -0
  66. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/tests/test_config.py +0 -0
  67. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/tests/test_graph_auth_provider.py +0 -0
  68. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/tests/test_graph_client.py +0 -0
  69. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/tests/test_graph_errors.py +0 -0
  70. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/tests/test_graph_serialize.py +1 -1
  71. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/tests/test_tools_mail_actions.py +0 -0
  72. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/tests/test_tools_mail_write.py +0 -0
  73. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/tests/test_tools_teams_channels.py +0 -0
  74. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/tests/test_tools_teams_chats.py +0 -0
  75. {msgraph_mcp_server-0.5.0 → msgraph_mcp_server-0.5.2}/tests/test_tools_util.py +0 -0
@@ -7,6 +7,12 @@ updates:
7
7
  groups:
8
8
  python-dependencies:
9
9
  patterns: ["*"]
10
+ # Groups only cover version updates unless told otherwise, so security
11
+ # fixes used to arrive as one PR per package, each rewriting uv.lock and
12
+ # conflicting with the rest.
13
+ python-security:
14
+ applies-to: security-updates
15
+ patterns: ["*"]
10
16
 
11
17
  - package-ecosystem: github-actions
12
18
  directory: /
@@ -19,7 +19,7 @@ jobs:
19
19
  steps:
20
20
  - uses: actions/checkout@v7
21
21
 
22
- - uses: astral-sh/setup-uv@v10.0.1
22
+ - uses: astral-sh/setup-uv@v10.1.0
23
23
  with:
24
24
  enable-cache: true
25
25
 
@@ -31,7 +31,7 @@ jobs:
31
31
  steps:
32
32
  - uses: actions/checkout@v7
33
33
 
34
- - uses: astral-sh/setup-uv@v10.0.1
34
+ - uses: astral-sh/setup-uv@v10.1.0
35
35
  with:
36
36
  enable-cache: true
37
37
 
@@ -46,7 +46,7 @@ jobs:
46
46
  steps:
47
47
  - uses: actions/checkout@v7
48
48
 
49
- - uses: astral-sh/setup-uv@v10.0.1
49
+ - uses: astral-sh/setup-uv@v10.1.0
50
50
  with:
51
51
  enable-cache: true
52
52
 
@@ -17,7 +17,7 @@ jobs:
17
17
  steps:
18
18
  - uses: actions/checkout@v7
19
19
 
20
- - uses: astral-sh/setup-uv@v10.0.1
20
+ - uses: astral-sh/setup-uv@v10.1.0
21
21
  with:
22
22
  enable-cache: true
23
23
 
@@ -40,7 +40,7 @@ jobs:
40
40
  - uses: actions/checkout@v7
41
41
 
42
42
  - name: Install uv
43
- uses: astral-sh/setup-uv@v10.0.1
43
+ uses: astral-sh/setup-uv@v10.1.0
44
44
 
45
45
  - name: Build sdist and wheel
46
46
  run: uv build
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: msgraph-mcp-server
3
- Version: 0.5.0
3
+ Version: 0.5.2
4
4
  Summary: MCP server for Microsoft Graph: Outlook mail and calendar, plus read-only Microsoft Teams
5
5
  Project-URL: Homepage, https://github.com/timfurlong/msgraph-mcp
6
6
  Project-URL: Repository, https://github.com/timfurlong/msgraph-mcp
@@ -19,7 +19,8 @@ Classifier: Programming Language :: Python :: 3.13
19
19
  Classifier: Topic :: Communications :: Email
20
20
  Classifier: Topic :: Office/Business :: Scheduling
21
21
  Requires-Python: >=3.11
22
- Requires-Dist: mcp>=1.0
22
+ Requires-Dist: mcp<3,>=2.2
23
+ Requires-Dist: microsoft-kiota-http<1.13,>=1.11.6
23
24
  Requires-Dist: msal>=1.31
24
25
  Requires-Dist: msgraph-sdk>=1.0
25
26
  Requires-Dist: python-dotenv>=1.0
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "msgraph-mcp-server"
3
- version = "0.5.0"
3
+ version = "0.5.2"
4
4
  description = "MCP server for Microsoft Graph: Outlook mail and calendar, plus read-only Microsoft Teams"
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -20,8 +20,15 @@ classifiers = [
20
20
  "Topic :: Office/Business :: Scheduling",
21
21
  ]
22
22
  dependencies = [
23
- "mcp>=1.0",
23
+ "mcp>=2.2,<3",
24
24
  "msgraph-sdk>=1.0",
25
+ # Transitive, bounded on purpose. kiota-http 1.13 moved request options
26
+ # from a request attribute into request.extensions, but msgraph-core (up to
27
+ # 1.5.1) only runs its middleware when that attribute exists. With 1.13+ the
28
+ # whole pipeline is skipped, so /me is never rewritten and every call fails
29
+ # with ErrorInvalidUser 'me-token-to-replace'. Drop this once msgraph-core
30
+ # reads the options from extensions.
31
+ "microsoft-kiota-http>=1.11.6,<1.13",
25
32
  "msal>=1.31",
26
33
  "python-dotenv>=1.0",
27
34
  ]
@@ -7,12 +7,12 @@
7
7
  "url": "https://github.com/timfurlong/msgraph-mcp",
8
8
  "source": "github"
9
9
  },
10
- "version": "0.5.0",
10
+ "version": "0.5.2",
11
11
  "packages": [
12
12
  {
13
13
  "registryType": "pypi",
14
14
  "identifier": "msgraph-mcp-server",
15
- "version": "0.5.0",
15
+ "version": "0.5.2",
16
16
  "runtimeHint": "uvx",
17
17
  "transport": {
18
18
  "type": "stdio"
@@ -6,12 +6,17 @@ get_access_token raises NotAuthenticatedError pointing at the login CLI.
6
6
 
7
7
  from __future__ import annotations
8
8
 
9
+ from mcp.server.mcpserver.exceptions import ToolError
10
+
9
11
  from msgraph_mcp import config
10
12
  from msgraph_mcp.auth.msal_app import build_app, persist_cache
11
13
 
12
14
 
13
- class NotAuthenticatedError(RuntimeError):
14
- """Raised when no valid token is available and silent refresh failed."""
15
+ class NotAuthenticatedError(ToolError, RuntimeError):
16
+ """Raised when no valid token is available and silent refresh failed.
17
+
18
+ A ToolError so MCPServer passes the login hint through to the caller.
19
+ """
15
20
 
16
21
 
17
22
  _LOGIN_HINT = "Not authenticated. Run `msgraph-mcp-login` in a terminal to sign in."
@@ -12,7 +12,6 @@ from pathlib import Path
12
12
 
13
13
  from dotenv import find_dotenv, load_dotenv
14
14
 
15
-
16
15
  # Load .env once at module import; process env takes precedence.
17
16
  #
18
17
  # The working-directory search runs first because that is where a user expects
@@ -15,7 +15,6 @@ from azure.core.credentials import AccessToken, TokenCredential
15
15
 
16
16
  from msgraph_mcp.auth.token import get_access_token
17
17
 
18
-
19
18
  # Short expiry: MSAL's cache handles real refresh. Each get_token call goes
20
19
  # through get_access_token() which calls acquire_token_silent (cheap, cached).
21
20
  _BUFFER_SECONDS = 300
@@ -200,7 +200,13 @@ class GraphBatchTransport:
200
200
  import json
201
201
 
202
202
  from kiota_abstractions.method import Method # type: ignore[import-untyped]
203
- from kiota_abstractions.request_information import RequestInformation # type: ignore[import-untyped]
203
+ from kiota_abstractions.request_information import (
204
+ RequestInformation, # type: ignore[import-untyped]
205
+ )
206
+ from msgraph.generated.models.o_data_errors.o_data_error import ODataError
207
+
208
+ from msgraph_mcp.auth.token import NotAuthenticatedError
209
+ from msgraph_mcp.graph.errors import map_kiota_error
204
210
 
205
211
  ri = RequestInformation()
206
212
  ri.url = self.GRAPH_BATCH_URL
@@ -208,7 +214,14 @@ class GraphBatchTransport:
208
214
  ri.headers.try_add("Content-Type", "application/json")
209
215
  ri.content = json.dumps(payload).encode("utf-8")
210
216
 
211
- raw = await self._graph.raw.request_adapter.send_primitive_async(
212
- ri, "bytes", {}
213
- )
217
+ # The error map lets kiota parse Graph's error body (code and message)
218
+ # when the $batch request as a whole fails.
219
+ try:
220
+ raw = await self._graph.raw.request_adapter.send_primitive_async(
221
+ ri, "bytes", {"XXX": ODataError}
222
+ )
223
+ except NotAuthenticatedError:
224
+ raise
225
+ except Exception as exc:
226
+ raise map_kiota_error(exc) from exc
214
227
  return json.loads(raw)
@@ -15,8 +15,9 @@ from msgraph_mcp.graph.auth_provider import MsalTokenCredential
15
15
 
16
16
 
17
17
  class GraphClient:
18
- def __init__(self) -> None:
19
- self._sdk = GraphServiceClient(
18
+ def __init__(self, sdk: GraphServiceClient | None = None) -> None:
19
+ # `sdk` lets tests supply a client wired to a mock transport.
20
+ self._sdk = sdk or GraphServiceClient(
20
21
  credentials=MsalTokenCredential(),
21
22
  scopes=config.SCOPES,
22
23
  )
@@ -3,18 +3,25 @@
3
3
  Kiota / msgraph-sdk raises a variety of exception shapes for HTTP errors.
4
4
  We normalize them into a single GraphAPIError class that the tool layer
5
5
  can catch and turn into a clean MCP tool error.
6
+
7
+ Both error classes subclass MCPServer's ToolError. MCPServer reports any other
8
+ exception as a bare "Error executing tool <name>" and keeps its text on the
9
+ server, so without that the caller would never see the Graph status, code and
10
+ message.
6
11
  """
7
12
 
8
13
  from __future__ import annotations
9
14
 
10
15
  from typing import Any
11
16
 
17
+ from mcp.server.mcpserver.exceptions import ToolError
18
+
12
19
 
13
- class GraphValidationError(RuntimeError):
20
+ class GraphValidationError(ToolError, RuntimeError):
14
21
  """Raised when tool params fail validation before the Graph call."""
15
22
 
16
23
 
17
- class GraphAPIError(RuntimeError):
24
+ class GraphAPIError(ToolError, RuntimeError):
18
25
  """Raised when Graph returns a non-success response."""
19
26
 
20
27
  def __init__(self, *, status: int, code: str, message: str) -> None:
@@ -12,7 +12,6 @@ from urllib.parse import urlparse
12
12
 
13
13
  from msgraph_mcp.graph.errors import GraphValidationError
14
14
 
15
-
16
15
  _ALLOWED_HOSTS = {"graph.microsoft.com"}
17
16
 
18
17
 
@@ -1,20 +1,19 @@
1
- """FastMCP server entry point.
1
+ """MCP server entry point.
2
2
 
3
3
  Boot order:
4
4
  1. Validate config (fail fast if env is missing).
5
5
  2. Build the GraphClient (lazy token acquisition — no Graph calls yet).
6
- 3. Build a FastMCP app, register all tools, and run on stdio.
6
+ 3. Build an MCPServer app, register all tools, and run on stdio.
7
7
  """
8
8
 
9
9
  from __future__ import annotations
10
10
 
11
- from mcp.server.fastmcp import FastMCP
11
+ from mcp.server.mcpserver import MCPServer
12
12
 
13
13
  from msgraph_mcp import config
14
14
  from msgraph_mcp.graph.client import GraphClient
15
15
  from msgraph_mcp.tools import register_all
16
16
 
17
-
18
17
  _INSTRUCTIONS = """
19
18
  Microsoft Outlook (mail + calendar) and Microsoft Teams (read-only) via Microsoft Graph.
20
19
 
@@ -45,12 +44,12 @@ that returns objects to also get the full Graph payload.
45
44
  """.strip()
46
45
 
47
46
 
48
- def build_server() -> FastMCP:
47
+ def build_server() -> MCPServer:
49
48
  # Fail fast on missing env (raises ConfigError before we hit the event loop)
50
49
  config.client_id()
51
50
  config.authority()
52
51
 
53
- mcp = FastMCP(name="msgraph-mcp", instructions=_INSTRUCTIONS)
52
+ mcp = MCPServer(name="msgraph-mcp", instructions=_INSTRUCTIONS)
54
53
  graph = GraphClient()
55
54
  register_all(mcp, graph=graph)
56
55
  return mcp
@@ -1,6 +1,6 @@
1
1
  """Tool registration entry point.
2
2
 
3
- register_all(mcp) wires every tool into the FastMCP app. Each tool module
3
+ register_all(mcp) wires every tool into the MCPServer app. Each tool module
4
4
  exposes a register(mcp, *, graph) function so we can keep modules small.
5
5
  """
6
6
 
@@ -14,8 +14,8 @@ from msgraph_mcp.tools import (
14
14
  mail_read,
15
15
  mail_rules,
16
16
  mail_write,
17
- teams_chats,
18
17
  teams_channels,
18
+ teams_chats,
19
19
  teams_content,
20
20
  util,
21
21
  )
@@ -1,7 +1,7 @@
1
1
  """Shared helpers for tools that return downloaded binary content.
2
2
 
3
3
  Image bytes are returned to the client as a native MCP image block
4
- (mcp.server.fastmcp.utilities.types.Image) so agents see the image directly
4
+ (mcp.server.mcpserver.Image) so agents see the image directly
5
5
  instead of a base64 text payload; save_path writes bytes to disk instead and
6
6
  returns only metadata. Non-image content without save_path falls back to the
7
7
  legacy base64 dict.
@@ -12,7 +12,7 @@ from __future__ import annotations
12
12
  import pathlib
13
13
  from typing import TypeGuard
14
14
 
15
- from mcp.server.fastmcp.utilities.types import Image
15
+ from mcp.server.mcpserver import Image
16
16
 
17
17
  _EXT_BY_CONTENT_TYPE = {
18
18
  "image/png": ".png",
@@ -31,7 +31,7 @@ def ext_for(content_type: str | None) -> str:
31
31
 
32
32
 
33
33
  def image_result(meta: dict, data: bytes, content_type: str) -> list:
34
- """Metadata dict + native MCP image block (FastMCP renders both)."""
34
+ """Metadata dict + native MCP image block (MCPServer renders both)."""
35
35
  return [meta, Image(data=data, format=content_type.removeprefix("image/"))]
36
36
 
37
37
 
@@ -47,7 +47,6 @@ from msgraph_mcp.graph.pagination import (
47
47
  from msgraph_mcp.graph.serialize import calendar_to_dict, event_to_dict
48
48
  from msgraph_mcp.graph.trimming import trim_calendar, trim_event
49
49
 
50
-
51
50
  _VALID_RESPONSES = {
52
51
  "accept": "accepted",
53
52
  "tentativelyAccept": "tentativelyAccepted",
@@ -73,7 +72,7 @@ async def list_calendars(
73
72
  collection = await graph.mailbox(mailbox).calendars.get()
74
73
  except NotAuthenticatedError:
75
74
  raise
76
- except Exception as exc: # noqa: BLE001
75
+ except Exception as exc:
77
76
  raise map_kiota_error(exc) from exc
78
77
  items = [
79
78
  trim_calendar(calendar_to_dict(c), include_raw=include_raw)
@@ -139,7 +138,7 @@ async def list_events(
139
138
  )
140
139
  except NotAuthenticatedError:
141
140
  raise
142
- except Exception as exc: # noqa: BLE001
141
+ except Exception as exc:
143
142
  raise map_kiota_error(exc) from exc
144
143
 
145
144
  items = [
@@ -167,7 +166,7 @@ async def get_event(
167
166
  evt = await graph.mailbox(mailbox).events.by_event_id(event_id).get()
168
167
  except NotAuthenticatedError:
169
168
  raise
170
- except Exception as exc: # noqa: BLE001
169
+ except Exception as exc:
171
170
  raise map_kiota_error(exc) from exc
172
171
  return trim_event(
173
172
  event_to_dict(evt), include_body=include_body, include_raw=include_raw
@@ -326,7 +325,7 @@ async def create_event(
326
325
  created = await target.events.post(evt)
327
326
  except NotAuthenticatedError:
328
327
  raise
329
- except Exception as exc: # noqa: BLE001
328
+ except Exception as exc:
330
329
  raise map_kiota_error(exc) from exc
331
330
  return trim_event(
332
331
  event_to_dict(created), include_body=False, include_raw=include_raw
@@ -396,7 +395,7 @@ async def update_event(
396
395
  updated = await graph.mailbox(mailbox).events.by_event_id(event_id).patch(patch)
397
396
  except NotAuthenticatedError:
398
397
  raise
399
- except Exception as exc: # noqa: BLE001
398
+ except Exception as exc:
400
399
  raise map_kiota_error(exc) from exc
401
400
  return trim_event(
402
401
  event_to_dict(updated), include_body=False, include_raw=include_raw
@@ -409,7 +408,7 @@ async def delete_event(*, graph, event_id: str, mailbox: str | None = None) -> d
409
408
  await graph.mailbox(mailbox).events.by_event_id(event_id).delete()
410
409
  except NotAuthenticatedError:
411
410
  raise
412
- except Exception as exc: # noqa: BLE001
411
+ except Exception as exc:
413
412
  raise map_kiota_error(exc) from exc
414
413
  return {"status": "deleted"}
415
414
 
@@ -425,7 +424,7 @@ async def cancel_event(
425
424
  await graph.mailbox(mailbox).events.by_event_id(event_id).cancel.post(body)
426
425
  except NotAuthenticatedError:
427
426
  raise
428
- except Exception as exc: # noqa: BLE001
427
+ except Exception as exc:
429
428
  raise map_kiota_error(exc) from exc
430
429
  return {"status": "cancelled"}
431
430
 
@@ -470,7 +469,7 @@ async def respond_to_event(
470
469
  await evt_builder.decline.post(decline_body)
471
470
  except NotAuthenticatedError:
472
471
  raise
473
- except Exception as exc: # noqa: BLE001
472
+ except Exception as exc:
474
473
  raise map_kiota_error(exc) from exc
475
474
  return {"status": _VALID_RESPONSES[response]}
476
475
 
@@ -536,7 +535,7 @@ async def find_meeting_times(
536
535
  result = await graph.mailbox(mailbox).find_meeting_times.post(body)
537
536
  except NotAuthenticatedError:
538
537
  raise
539
- except Exception as exc: # noqa: BLE001
538
+ except Exception as exc:
540
539
  raise map_kiota_error(exc) from exc
541
540
 
542
541
  suggestions_raw = getattr(result, "meeting_time_suggestions", None) or []
@@ -31,7 +31,7 @@ async def list_folders(
31
31
  collection = await graph.mailbox(mailbox).mail_folders.get()
32
32
  except NotAuthenticatedError:
33
33
  raise
34
- except Exception as exc: # noqa: BLE001
34
+ except Exception as exc:
35
35
  raise map_kiota_error(exc) from exc
36
36
  items = [
37
37
  trim_folder(folder_to_dict(f), include_raw=include_raw)
@@ -70,7 +70,7 @@ async def move_message(
70
70
  )
71
71
  except NotAuthenticatedError:
72
72
  raise
73
- except Exception as exc: # noqa: BLE001
73
+ except Exception as exc:
74
74
  raise map_kiota_error(exc) from exc
75
75
  return trim_message(
76
76
  message_to_dict(moved), include_body=False, include_raw=include_raw
@@ -113,7 +113,7 @@ async def create_folder(
113
113
  created = await graph.mailbox(mailbox).mail_folders.post(body)
114
114
  except NotAuthenticatedError:
115
115
  raise
116
- except Exception as exc: # noqa: BLE001
116
+ except Exception as exc:
117
117
  raise map_kiota_error(exc) from exc
118
118
  return trim_folder(folder_to_dict(created), include_raw=include_raw)
119
119
 
@@ -166,7 +166,7 @@ async def update_folder(
166
166
  result = await folder_endpoint.move.post(move_body)
167
167
  except NotAuthenticatedError:
168
168
  raise
169
- except Exception as exc: # noqa: BLE001
169
+ except Exception as exc:
170
170
  raise map_kiota_error(exc) from exc
171
171
  return trim_folder(folder_to_dict(result), include_raw=include_raw)
172
172
 
@@ -198,7 +198,7 @@ async def delete_folder(
198
198
  folder = await folder_endpoint.get()
199
199
  except NotAuthenticatedError:
200
200
  raise
201
- except Exception as exc: # noqa: BLE001
201
+ except Exception as exc:
202
202
  raise map_kiota_error(exc) from exc
203
203
  total = getattr(folder, "total_item_count", None) or 0
204
204
  children = getattr(folder, "child_folder_count", None) or 0
@@ -212,7 +212,7 @@ async def delete_folder(
212
212
  await folder_endpoint.delete()
213
213
  except NotAuthenticatedError:
214
214
  raise
215
- except Exception as exc: # noqa: BLE001
215
+ except Exception as exc:
216
216
  raise map_kiota_error(exc) from exc
217
217
  return {"deleted": True, "id": folder_id}
218
218
 
@@ -42,13 +42,21 @@ def _list_messages_query(*, limit: int, filter_expr: str | None = None):
42
42
  ](query_parameters=qp)
43
43
 
44
44
 
45
+ # Graph refuses a $filter combined with $orderby unless the filter names the
46
+ # sort property first (400 InefficientFilter, "The restriction or sort order is
47
+ # too complex for this operation"). Leading every filter with this always-true
48
+ # clause satisfies that rule, so filtered results keep the server-side
49
+ # newest-first order and stay newest first across pages.
50
+ _SORT_CLAUSE = "receivedDateTime ge 1900-01-01T00:00:00Z"
51
+
52
+
45
53
  def _build_list_filter(*, unread_only: bool, filter: str | None) -> str | None:
46
54
  parts: list[str] = []
47
55
  if unread_only:
48
56
  parts.append("isRead eq false")
49
57
  if filter:
50
58
  parts.append(f"({filter})")
51
- return " and ".join(parts) if parts else None
59
+ return " and ".join([_SORT_CLAUSE, *parts]) if parts else None
52
60
 
53
61
 
54
62
  def _search_query(*, limit: int, query: str):
@@ -74,6 +82,12 @@ async def list_messages(
74
82
  ) -> dict:
75
83
  """List messages from a mail folder, newest first.
76
84
 
85
+ Filtered results are newest first too. When `filter` or `unread_only` is
86
+ given, the server-side $filter starts with
87
+ `receivedDateTime ge 1900-01-01T00:00:00Z and`, because Graph only sorts a
88
+ filtered query by receivedDateTime when the filter names it first. That
89
+ clause matches every message, so it doesn't narrow the results.
90
+
77
91
  Args:
78
92
  folder_id: Folder id, or a well-known name (inbox, sentitems, drafts,
79
93
  deleteditems, archive, junkemail). Default: inbox.
@@ -109,7 +123,7 @@ async def list_messages(
109
123
  )
110
124
  except NotAuthenticatedError:
111
125
  raise
112
- except Exception as exc: # noqa: BLE001
126
+ except Exception as exc:
113
127
  raise map_kiota_error(exc) from exc
114
128
 
115
129
  items = [
@@ -166,7 +180,7 @@ async def search_messages(
166
180
  )
167
181
  except NotAuthenticatedError:
168
182
  raise
169
- except Exception as exc: # noqa: BLE001
183
+ except Exception as exc:
170
184
  raise map_kiota_error(exc) from exc
171
185
 
172
186
  items = [
@@ -205,7 +219,7 @@ async def get_message(
205
219
  msg = await graph.mailbox(mailbox).messages.by_message_id(message_id).get()
206
220
  except NotAuthenticatedError:
207
221
  raise
208
- except Exception as exc: # noqa: BLE001
222
+ except Exception as exc:
209
223
  raise map_kiota_error(exc) from exc
210
224
  return trim_message(
211
225
  message_to_dict(msg), include_body=include_body, include_raw=include_raw
@@ -228,7 +242,7 @@ async def list_attachments(
228
242
  )
229
243
  except NotAuthenticatedError:
230
244
  raise
231
- except Exception as exc: # noqa: BLE001
245
+ except Exception as exc:
232
246
  raise map_kiota_error(exc) from exc
233
247
  items = [
234
248
  trim_attachment_list(
@@ -270,7 +284,7 @@ async def download_attachment(
270
284
  )
271
285
  except NotAuthenticatedError:
272
286
  raise
273
- except Exception as exc: # noqa: BLE001
287
+ except Exception as exc:
274
288
  raise map_kiota_error(exc) from exc
275
289
 
276
290
  content = getattr(att, "content_bytes", None)
@@ -35,7 +35,7 @@ async def list_rules(
35
35
  collection = await _rules_endpoint(graph, mailbox).get()
36
36
  except NotAuthenticatedError:
37
37
  raise
38
- except Exception as exc: # noqa: BLE001
38
+ except Exception as exc:
39
39
  raise map_kiota_error(exc) from exc
40
40
  items = [
41
41
  trim_message_rule(message_rule_to_dict(r), include_raw=include_raw)
@@ -63,7 +63,7 @@ async def get_rule(
63
63
  rule = await _rules_endpoint(graph, mailbox).by_message_rule_id(rule_id).get()
64
64
  except NotAuthenticatedError:
65
65
  raise
66
- except Exception as exc: # noqa: BLE001
66
+ except Exception as exc:
67
67
  raise map_kiota_error(exc) from exc
68
68
  return trim_message_rule(message_rule_to_dict(rule), include_raw=include_raw)
69
69
 
@@ -241,7 +241,7 @@ async def create_rule(
241
241
  created = await _rules_endpoint(graph, mailbox).post(body)
242
242
  except NotAuthenticatedError:
243
243
  raise
244
- except Exception as exc: # noqa: BLE001
244
+ except Exception as exc:
245
245
  raise map_kiota_error(exc) from exc
246
246
  return trim_message_rule(message_rule_to_dict(created), include_raw=include_raw)
247
247
 
@@ -346,7 +346,7 @@ async def update_rule(
346
346
  )
347
347
  except NotAuthenticatedError:
348
348
  raise
349
- except Exception as exc: # noqa: BLE001
349
+ except Exception as exc:
350
350
  raise map_kiota_error(exc) from exc
351
351
  return trim_message_rule(message_rule_to_dict(updated), include_raw=include_raw)
352
352
 
@@ -367,7 +367,7 @@ async def delete_rule(*, graph, rule_id: str, mailbox: str | None = None) -> dic
367
367
  await _rules_endpoint(graph, mailbox).by_message_rule_id(rule_id).delete()
368
368
  except NotAuthenticatedError:
369
369
  raise
370
- except Exception as exc: # noqa: BLE001
370
+ except Exception as exc:
371
371
  raise map_kiota_error(exc) from exc
372
372
  return {"deleted": True, "id": rule_id}
373
373
 
@@ -33,7 +33,6 @@ from msgraph_mcp.graph.errors import GraphValidationError, map_kiota_error
33
33
  from msgraph_mcp.graph.serialize import message_to_dict
34
34
  from msgraph_mcp.graph.trimming import trim_message
35
35
 
36
-
37
36
  _MAX_INLINE_ATTACHMENT_BYTES = 3 * 1024 * 1024
38
37
 
39
38
 
@@ -153,7 +152,7 @@ async def send_message(
153
152
  await graph.mailbox(mailbox).send_mail.post(req)
154
153
  except NotAuthenticatedError:
155
154
  raise
156
- except Exception as exc: # noqa: BLE001
155
+ except Exception as exc:
157
156
  raise map_kiota_error(exc) from exc
158
157
  return {"status": "sent"}
159
158
 
@@ -185,7 +184,7 @@ async def create_draft(
185
184
  created = await graph.mailbox(mailbox).messages.post(msg)
186
185
  except NotAuthenticatedError:
187
186
  raise
188
- except Exception as exc: # noqa: BLE001
187
+ except Exception as exc:
189
188
  raise map_kiota_error(exc) from exc
190
189
  return trim_message(
191
190
  message_to_dict(created), include_body=False, include_raw=include_raw
@@ -235,7 +234,7 @@ async def reply_message(
235
234
  )
236
235
  except NotAuthenticatedError:
237
236
  raise
238
- except Exception as exc: # noqa: BLE001
237
+ except Exception as exc:
239
238
  raise map_kiota_error(exc) from exc
240
239
  return {"status": "sent"}
241
240
 
@@ -256,7 +255,7 @@ async def reply_all_message(
256
255
  )
257
256
  except NotAuthenticatedError:
258
257
  raise
259
- except Exception as exc: # noqa: BLE001
258
+ except Exception as exc:
260
259
  raise map_kiota_error(exc) from exc
261
260
  return {"status": "sent"}
262
261
 
@@ -280,7 +279,7 @@ async def forward_message(
280
279
  )
281
280
  except NotAuthenticatedError:
282
281
  raise
283
- except Exception as exc: # noqa: BLE001
282
+ except Exception as exc:
284
283
  raise map_kiota_error(exc) from exc
285
284
  return {"status": "sent"}
286
285
 
@@ -343,7 +342,7 @@ async def update_message(
343
342
  )
344
343
  except NotAuthenticatedError:
345
344
  raise
346
- except Exception as exc: # noqa: BLE001
345
+ except Exception as exc:
347
346
  raise map_kiota_error(exc) from exc
348
347
  return trim_message(
349
348
  message_to_dict(updated), include_body=False, include_raw=include_raw
@@ -356,7 +355,7 @@ async def delete_message(*, graph, message_id: str, mailbox: str | None = None)
356
355
  await graph.mailbox(mailbox).messages.by_message_id(message_id).delete()
357
356
  except NotAuthenticatedError:
358
357
  raise
359
- except Exception as exc: # noqa: BLE001
358
+ except Exception as exc:
360
359
  raise map_kiota_error(exc) from exc
361
360
  return {"status": "deleted"}
362
361