本地Lambda函数几行代码实现MCP Server

文章视频教程: https://www.bilibili.com/video/BV1wWd8YNEZY/?vd_source=e14a954d1c7739b152548d58bf125603

MCP+Lambda

MCP服务器充当MCP客户端与Lambda函数之间的桥梁,使生成式AI模型能够访问和运行Lambda函数作为工具。

社区项目 https://github.com/danilop/MCP2Lambda

但是目前MCP在安全方面还比较弱,所以社区MCP工具基本都是本地工具,Lambda能不能也本地部署。

Refunc是基于K8S的一个本地化的Lambda平台,可以实现本地部署Lambda函数。

MCP+Refunc

Refunc社区跟进MCP发展,实现了任意函数通过MCP开放调用。

https://github.com/refunc/mcp-server

部署 Refunc

refunc-system 命名空间下部署所有 Refunc 组件。

kubectl apply -f https://raw.githubusercontent.com/refunc/mcp-server/refs/heads/main/deploy/play-all-in-one.yaml

接着,将 Refunc 服务代理到本地。

kubectl port-forward svc/aws-api 8000:http --address 0.0.0.0 -n refunc-system
kubectl port-forward svc/mcp-server 8001:http --address 0.0.0.0 -n refunc-system

注意:演示部署中的数据是非持久化的。

创建函数

使用 refunc-cli 创建一个函数。

pip install -U refunc-cli
mkdir mcp-demo && cd mcp-demo
rfctl init

main.py 中编写任意代码,演示将回显一条问候消息。

AWS_DEFAULT_ENDPOINT=http://127.0.0.1:8000 rfctl create

注意:您需要一个 Python 3 环境。

创建 MCP 端点

编辑 lambda.yaml 并粘贴以下代码。

metadata:
  name: mcp-demo
  namespace: refunc-system
spec:
  build:
    source: .
    manifest: requirements.txt
    language: python
    architecture: x86_64
  handler: main.lambda_handler
  timeout: 120
  runtime: "python3.10"
  concurrency: 1
  environment:
    ENV_TEST: TEST
#  url:
#    cors:
#      allowCredentials: true
#      allowHeaders: "*"
#      allowMethods: "*"
#      allowOrigins: "*"
#      exposeHeaders: "*"
#      maxAge: 300
  events:
    # - name: hourly
    #   type: cron
    #   mapping:
    #     cron: 0 * * * *
    #     location: Asia/Shanghai
    #     args:
    #       var1: value1
    #     saveLog: false
    #     saveResult: false
    - name: mcp
      type: mcp
      mapping:
        args:
          token: mcp-demo
          tools:
            - name: echo-hello
              desc: Echo a hello msg
              schema:
                type: object
                properties: {}
                required: []
        saveLog: false
        saveResult: false

接下来,运行以下命令更新配置:

AWS_DEFAULT_ENDPOINT=http://127.0.0.1:8000 rfctl update-config

演示的 MCP SSE 端点为:http://127.0.0.1:8001/refunc-system/mcp-demo/test/mcp-demo/sse

Refunc MCP SSE URL 路径格式为:/namespace/<token-secret-name>/<token>/<func-name>/sse

Refunc MCP 事件规范

- name: mcp
  type: mcp # 事件类型必须为 mcp。
  mapping:
    args:
      token: mcp-demo # 可以在 play-all-in-one.yaml 中找到的 token 秘钥名称。
      tools: # MCP 工具
        - name: echo-hello # MCP 工具名称
          desc: Echo a hello msg # MCP 工具描述
          schema: # MCP 工具参数,用有效的 JSON Schema 描述。
            type: object
            properties: {}
            required: []
    saveLog: false
    saveResult: false

事件的tools字段是一个数组,您可以通过一个函数实现多个tool。在调用函数时,Refunc 会添加两个内置参数:_call_type_call_method,其中 _call_method 是tool的名称。_call_type会表示调用来自MCP的什么类型,Tool、Resource或者Prompts。

阅读全文

安伯尼可RG40xxV掌机设置远程管理游戏

开启SSH

使用系统自带的文件管理器,编辑系统设置文件

/mnt/mod/ctrl/configs/system.cfg

添加开启ssh配置

global.ssh=1

然后重启,连接WIFI,从系统菜单配置获取IP地址。

进入一个游戏,然后暂停,不然系统自动锁屏休眠,网络会断,配置好FTP以后传游戏时候也这样操作。

安装VSFTP服务器

使用xshell或者其他ssh终端工具登录,密码同用户名(猜出来的),建议登录上去之后改掉,实测不影响系统使用。

ssh root@192.168.x.x

会发现周哥的系统就是原生的Ubuntu。

Welcome to Ubuntu 22.04 LTS (GNU/Linux 4.9.170 aarch64)

通过apt安装ftp服务器。

# 系统默认用的清华的源666
apt update
apt install vsftpd

配置VSFTP

直接在终端下vim配

mv /etc/vsftpd.conf /etc/vsftpd.conf.bak
vim /etc/vsftpd.conf

vsftp配置如下

listen=YES
anonymous_enable=NO
local_enable=YES
write_enable=YES
chroot_local_user=YES
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd/chroot_list
local_root=/mnt
allow_writeable_chroot=YES
pasv_enable=YES

开启root使用ftp登录

vim /etc/ftpusers
删除或注释掉root行

重启vsftp

systemctl restart vsftpd

通过FTP管理游戏文件

下载FileZilla,FTP连接游戏机IP即可,FTP客户端使用参考其他教程。

系统卡目录

/mmc

扩展卡目录

/sdcard
阅读全文

Docker部署MySQL主从复制

运维现场

已有一个通过Docker部署的MySQL单实例,实例中有多个库,且已有数据,备份容灾目的现扩充从库。

docker run -d --name mysql --restart unless-stopped -p 3406:3306 --log-opt max-size=50m \
-v /data/mysql:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=xxx \
mysql:5.7

主库调整

The default configuration for MySQL can be found in /etc/mysql/my.cnf, which may !includedir additional directories such as /etc/mysql/conf.d or /etc/mysql/mysql.conf.d.

MySQL的镜像已经把主配置my.cnf映射到/etc/mysql/conf.d目录,所以只需自定义一个my.conf,通过目录挂载到容器里面即可。

[mysqld]
skip-host-cache
skip-name-resolve
server_id=1  //设置服务id
log_bin=mysql-bin //开启二进制日志
binlog-ignore-db=mysql
binlog_cache_size=1M
binlog_format=mixed
expire_logs_days=7
slave_skip_errors=1062

[mysql]

[mysqldump]
quick
quote-names
max_allowed_packet=16M

停止当前容器,修改启动命令,挂载my.cnf,重新启动。

docker run -d --name mysql --restart unless-stopped -p 3406:3306 --log-opt max-size=50m \
-v /data/mysql:/var/lib/mysql \
-v /data/mysql.conf.d:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=xxx \
mysql:5.7

查看master状态。

MySQL [(none)]> show master status;
+------------------+-----------+--------------+------------------+-------------------+
| File             | Position  | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+-----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 452181159 |              | mysql            |                   |
+------------------+-----------+--------------+------------------+-------------------+

从库搭建

配置slave的my.cnf,在另外一台机器上,启动一个空实例。

[mysqld]
skip-host-cache
skip-name-resolve
server_id=2
log_bin=slave-bin
binlog-ignore-db=mysql
binlog_cache_size=1M
binlog_format=mixed
expire_logs_days=7
slave_skip_errors=1062
relay_log=slave-relay-bin
log_slave_updates=1
read_only=1

[mysql]

[mysqldump]
quick
quote-names
max_allowed_packet=16M
docker run -d --name mysql --restart unless-stopped -p 3406:3306 --log-opt max-size=50m \
-v /data/mysql:/var/lib/mysql \
-v /data/mysql.conf.d:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=xxx \
mysql:5.7

全量备份主库

首先锁表。

#全备主库时需要另开一个终端,给数据库加上读锁,避免在备份期间有其他人在写入导致数据不一致
#此锁表的终端必须在备份完成以后才能退出
mysql> flush tables with read lock;

记录binlog状态,锁表之后binlog的Position就不会再变了,记录下File和Position。

MySQL [(none)]> show master status;
+------------------+-----------+--------------+------------------+-------------------+
| File             | Position  | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+-----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 461472295 |              | mysql            |                   |
+------------------+-----------+--------------+------------------+-------------------+

操作备份,备份完成,即可释放表锁。

mysqldump -h 172.x.x.10 -P 3406 -uroot -pxxx --all-databases > all-database-$(date '+%F-%H-%M-%S').sql

#解除主库的锁表状态,直接退出交互式界面即可
mysql> quit

把全量数据导入到从库中

mysql -h 172.x.x.11 -P 3406 -uroot -pxxx < all-database-2024-10-09-11-27-41.sql

配置从库同步

登录从库,执行change master,注意master_log_file和master_log_pos,填写在备份主库前记录值。

change master to master_host='172.x.x.10',master_port=3406,master_user='root',master_password='xxx',master_log_file='mysql-bin.000002',master_log_pos=461472295;

查看从库状态。

MySQL [(none)]> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.x.x.10
                  Master_User: root
                  Master_Port: 3406
                Connect_Retry: 30
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 479860795
               Relay_Log_File: slave-relay-bin.000003
                Relay_Log_Pos: 111190829
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: No
            Slave_SQL_Running: No
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 479860795
              Relay_Log_Space: 432038788
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
                  Master_UUID: cd0f01f3-27c4-11ef-9394-0242ac130002
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:

可以看到Slave_IO_Running和Slave_SQL_Running,开始为No,需要执行slave start。

mysql> slave start
mysql> show slave status \G;
...
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
...

验证同步

通过主库创建库表,写入数据,验证从库同步。

阅读全文

StableDiffusion

Models

CheckPoint

WebUI

# ubuntu 22.04
sudo apt install libgl1 libglib2.0-0 libgoogle-perftools4 libtcmalloc-minimal4
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui
# 切换到指定版本
git checkout -b v1-10-1 tags/v1.10.1
./webui.sh
# chinese interface
https://github.com/VinsonLaro/stable-diffusion-webui-chinese
阅读全文

WSL2使用CUDA

安装

windows安装新驱动,wsl2内部不需要安装驱动,直接安装cuda-toolkit

wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pin
sudo mv cuda-wsl-ubuntu.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/12.8.1/local_installers/cuda-repo-wsl-ubuntu-12-8-local_12.8.1-1_amd64.deb
sudo dpkg -i cuda-repo-wsl-ubuntu-12-8-local_12.8.1-1_amd64.deb
sudo cp /var/cuda-repo-wsl-ubuntu-12-8-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cuda-toolkit-12-8

nvidia-smi使用

ln -s nvidia-smi -> /usr/lib/wsl/lib/nvidia-smi

参考文档

https://learn.microsoft.com/en-us/windows/ai/directml/gpu-cuda-in-wsl
https://docs.nvidia.com/cuda/wsl-user-guide/index.html#wsl-2-support-constraints
https://developer.nvidia.com/cuda-downloads?target_os=Linux&target_arch=x86_64&Distribution=WSL-Ubuntu&target_version=2.0&target_type=deb_local

阅读全文