-
Notifications
You must be signed in to change notification settings - Fork 3
Feat: add mcp server #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
043582f
feat: add mcp server
lukasdotcom 1319bec
add multi user support
lukasdotcom d99f46e
function wrapper
lukasdotcom 6c5f543
implement user auth with app password in bearer
lukasdotcom 17cc117
fastmcp stable version
lukasdotcom 7677952
remove debug
lukasdotcom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| # SPDX-FileCopyrightText: 2024 LangChain, Inc. | ||
| # SPDX-License-Identifier: MIT | ||
| import time | ||
| from functools import wraps | ||
|
|
||
| from fastmcp.server.dependencies import get_context | ||
| from nc_py_api import NextcloudApp | ||
| from fastmcp.server.middleware import Middleware, MiddlewareContext, CallNext | ||
| from fastmcp.tools import Tool | ||
| from mcp import types as mt | ||
| from ex_app.lib.tools import get_tools | ||
| import requests | ||
|
|
||
| def get_user(authorization_header: str, nc: NextcloudApp) -> str: | ||
| response = requests.get( | ||
| f"{nc.app_cfg.endpoint}/ocs/v2.php/cloud/user", | ||
| headers={ | ||
| "Accept": "application/json", | ||
| "Ocs-Apirequest": "1", | ||
| "Authorization": authorization_header, | ||
| }, | ||
| ) | ||
| if response.status_code != 200: | ||
| raise Exception("Failed to get user info") | ||
| return response.json()["ocs"]["data"]["id"] | ||
|
|
||
|
|
||
| class UserAuthMiddleware(Middleware): | ||
| async def on_message(self, context: MiddlewareContext, call_next): | ||
| # Middleware stores user info in context state | ||
| authorization_header = context.fastmcp_context.request_context.request.headers.get("Authorization") | ||
| if authorization_header is None: | ||
| raise Exception("Authorization header is missing/invalid") | ||
| nc = NextcloudApp() | ||
| user = get_user(authorization_header, nc) | ||
| nc.set_user(user) | ||
| context.fastmcp_context.set_state("nextcloud", nc) | ||
| return await call_next(context) | ||
|
|
||
|
|
||
| LAST_MCP_TOOL_UPDATE = 0 | ||
|
|
||
|
|
||
| class ToolListMiddleware(Middleware): | ||
| def __init__(self, mcp): | ||
| self.mcp = mcp | ||
|
|
||
| async def on_message( | ||
| self, | ||
| context: MiddlewareContext[mt.ListToolsRequest], | ||
| call_next: CallNext[mt.ListToolsRequest, list[Tool]], | ||
| ) -> list[Tool]: | ||
| global LAST_MCP_TOOL_UPDATE | ||
| if LAST_MCP_TOOL_UPDATE + 60 < time.time(): | ||
| safe, dangerous = await get_tools(context.fastmcp_context.get_state("nextcloud")) | ||
| tools = await self.mcp.get_tools() | ||
| if LAST_MCP_TOOL_UPDATE + 60 < time.time(): | ||
| for tool in tools.keys(): | ||
| self.mcp.remove_tool(tool) | ||
| for tool in safe + dangerous: | ||
| if not hasattr(tool, "func") or tool.func is None: | ||
| continue | ||
| self.mcp.tool()(mcp_tool(tool.func)) | ||
| LAST_MCP_TOOL_UPDATE = time.time() | ||
lukasdotcom marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return await call_next(context) | ||
|
|
||
| # Regenerates the tools with the correct nc object | ||
marcelklehr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| def mcp_tool(tool): | ||
| @wraps(tool) | ||
| async def wrapper(*args, **kwargs): | ||
| ctx = get_context() | ||
| nc = ctx.get_state('nextcloud') | ||
| safe, dangerous = await get_tools(nc) | ||
| tools = safe + dangerous | ||
| for t in tools: | ||
| if hasattr(t, "func") and t.func and t.name == tool.__name__: | ||
| return t.func(*args, **kwargs) | ||
| raise RuntimeError("Tool not found") | ||
| return wrapper | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.