Skip to content

[OSPP] Implement a mcp server to run any scripts or commands securely#3547

Merged
Aias00 merged 52 commits intoapache:masterfrom
yexuanyang:master
Sep 22, 2025
Merged

[OSPP] Implement a mcp server to run any scripts or commands securely#3547
Aias00 merged 52 commits intoapache:masterfrom
yexuanyang:master

Conversation

@yexuanyang
Copy link
Contributor

@yexuanyang yexuanyang commented Jul 4, 2025

What's changed?

Hello, everyone! Currently, we are looking at monitoring data or alarm data on hertzbeat platform, but we can't make the next step such as automatic recovery, automatic repair, etc. for alarm data or collected data. Now with AI's capabilities, we can automate monitoring and alerting with the help of AI, which generates and decides on its own the repair scripts or tasks for abnormal services on the other end based on the alert information, monitoring information, etc., and then calls the mcp server deployed on the other end to execute them. So I wrote a mcp server and put it under the mcp-servers folder, other mcp servers can be put into this same directory, so that it is easy to add new mcp servers to hertzbeat.

This mcp server currently accomplishes the following:

  • Execute scripts through the mcp server. Scripts are currently executed from unix-like os using the shibang #! ASCII text executable file in unix-like os using shibang #! in the header, windows bat format is not tested.
  • There are preset tools that combine a series of common commands to accomplish some information gathering tasks. For example, getting the current disk usage.
  • Record logs of command and script execution. There may be omissions in the logging of some operations, but this is only the first version.
  • Command blacklist and operator blacklist. Me. Commands and operators in the blacklist cannot be executed; the blacklist is now configured in config.yaml.

Some things that need to be done in the future:

  • Command whitelisting. Some commands may have dangerous operators and commands, but administrators still want to allow such commands to be executed, these commands are put into a whitelist, and the program prioritizes checking to see if the command matches one of the whitelisted items, and executes it directly if it matches, and only checks with the blacklist if it doesn't exist.
  • documentation. Current The documentation is incomplete in the current code, and needs to be filled in later.

Sorry, this pr might be a bit big, I'll try to work with reviewer to ease the pain of review.

Checklist

  • I have read the Contributing Guide
  • I have written the necessary doc or comment.
  • I have added the necessary unit tests and all cases have passed.

@github-actions github-actions bot added the doc Improvements or additions to documentation label Jul 4, 2025
@tomsun28 tomsun28 added the OSPP label Jul 4, 2025
@tomsun28 tomsun28 changed the title [feature] Implement a mcp server to run any scripts or commands securely [OSPP] Implement a mcp server to run any scripts or commands securely Jul 4, 2025
@tomsun28 tomsun28 requested a review from Aias00 July 5, 2025 09:00
Aias00 and others added 6 commits July 10, 2025 09:29
Validator will check the whitelist firstly and then
check the blacklist.
Add some comment in config.toml.

Whitelist have 'commands' and 'regex' settings,
validator full matches the commands and uses the rust
 crate `regex` to match strings in 'regex'
@yexuanyang
Copy link
Contributor Author

Latest developments:

  • Command whitelisting. Validator supports the whitelist verification now.
  • Documentation. Add many documentation comments now.

Unit tests will be added soon.

**Summary**

Added 67 unit tests covering all core modules with 100% pass rate.

**Test Coverage**

- **Config (6 tests)**: TOML parsing, validation, error handling
- **Validator (13 tests)**: Command security, whitelist/blacklist, regex patterns
- **OAuth (17 tests)**: Authentication flow, token management, security validation
- **Bash Server (17 tests)**: Command execution, timeout handling, serialization
- **Main (9 tests)**: HTTP handlers, middleware, OAuth mock tests
- **Additional OAuth Mock (5 tests)**: Store functionality, authorization flow, token exchange, middleware, error handling
Aias00 and others added 5 commits August 19, 2025 18:39
Co-authored-by: Yang Yexuan <yyxrust@bupt.edu.cn>
Signed-off-by: aias00 <liuhongyu@apache.org>
Co-authored-by: Yang Yexuan <yyxrust@bupt.edu.cn>
Signed-off-by: aias00 <liuhongyu@apache.org>
@Aias00
Copy link
Contributor

Aias00 commented Aug 31, 2025

add ci test for ur code, pls

@github-actions github-actions bot added the infra label Sep 1, 2025
@yexuanyang
Copy link
Contributor Author

I have added CI tests

@Aias00 Aias00 marked this pull request as ready for review September 1, 2025 13:03
@Aias00
Copy link
Contributor

Aias00 commented Sep 1, 2025

hi, codecov is no needed, remove this for fixing ci

@yexuanyang
Copy link
Contributor Author

I didn't add the backend-build workflow which has codecov action and I don't know why these code can not pass the check.

@yexuanyang
Copy link
Contributor Author

It seems to be the bug in action codecov@v4, read this codecov/codecov-action#1280 . Retry the check maybe ok.

@yexuanyang
Copy link
Contributor Author

Maybe try changing the codecov-action to 4.4.0?

@Aias00
Copy link
Contributor

Aias00 commented Sep 2, 2025

Maybe try changing the codecov-action to 4.4.0?

rerun it, and ci passed 😂

@tomsun28 tomsun28 added this to the 1.8.0 milestone Sep 16, 2025
Copy link
Contributor

@Aias00 Aias00 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thx

@Aias00 Aias00 merged commit 586aa53 into apache:master Sep 22, 2025
5 checks passed
@github-project-automation github-project-automation bot moved this from To do to Done in Apache HertzBeat Sep 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

doc Improvements or additions to documentation good first pull request Good for newcomers infra new feature OSPP

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

6 participants